jQuery if “this” contains

前端 未结 4 694
予麋鹿
予麋鹿 2021-01-31 18:19

I\'m trying to change any elements containing a particular text string to a red color. In my example I can get the child elements to become blue, but there\'s something about th

4条回答
  •  不知归路
    2021-01-31 19:20

    you can use match to find the text inside the particular element

    $('#main-content-panel .entry').each(function() {
        $(this).css('color', 'blue');      
    });			
    
    $('#main-content-panel .entry').each(function() {
        if($(this).text().match('Replace Me')) {
            $(this).css('color', 'red'); 
        }      
    });
    
    
    ABC
    ABC Replace Me
    ABC
    ABC Replace Me

提交回复
热议问题