Change the color of a text in div using jquery contains

后端 未结 6 738
别那么骄傲
别那么骄傲 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:09

    Could be done like that:

    http://jsfiddle.net/PELkt/

    var search = 'bar';
    $(document).ready(function () {
        $("div:contains('"+search+"')").each(function () {
            var regex = new RegExp(search,'gi');
            $(this).html($(this).text().replace(regex, ""+search+""));
        });
    });
    

提交回复
热议问题