Change the color of a text in div using jquery contains

后端 未结 6 838
别那么骄傲
别那么骄傲 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条回答
  •  萌比男神i
    2021-01-13 08:10

    Solution with code snippet..

    var text_change = 'bar';
    $(document).ready(function () {
        $("div:contains('"+text_change+"')").each(function () {
            var regex = new RegExp(text_change,'gi');
            $(this).html($(this).text().replace(regex, ""+text_change+""));
        });
    });
    .red {
        color:#008000;
    }
    
    
    this is a new bar and bar.

提交回复
热议问题