Change the color of a text in div using jquery contains

后端 未结 6 723
别那么骄傲
别那么骄傲 2021-01-13 07:47

Here the whole text inside the div get\'s red color. but I need only the \"bar\" word color to be changed



        
6条回答
  •  有刺的猬
    2021-01-13 08:26

    You can do like this.

    $(document).ready(function(){
        var matchingTest = 'demo';
        $("#container:contains("+matchingTest+")").each(function () {
    	    $(this).html($(this).html().replace(matchingTest, ""+matchingTest+""));
        });
    });
    .red{
      background:#eada93;
    }
    
    
    Hello this is a demo contents.

提交回复
热议问题