jQuery find exact HTML content

前端 未结 5 780
死守一世寂寞
死守一世寂寞 2021-01-28 13:06

I am using the following script to find all h1 elements, within a parent ID container that will fit a curtain criteria...

$(\'#cpcompheader h1\').html(\"&nbs         


        
相关标签:
5条回答
  • 2021-01-28 13:43

    You could try finding all the h1 tags and then checking if they contain a certain value.

    $('#yourParent h1').each(function(){
        if($(this).html() == " "){
            // magic
        }
    });
    
    0 讨论(0)
  • 2021-01-28 13:45

    you could filter your element using regex, and then remove it if dont have any value

    $('h1').each(function(){
      var filtered = $(this).html($(this).html().replace(/ /gi,''));     
      if($(filtered).html() === ''){
        $(filtered).remove();
      }
    });
    

    here a demo

    0 讨论(0)
  • 2021-01-28 13:48

    I guess you could do something like:

    $('h1:contains( )');
    

    or if you want the exact match:

    $('h1').filter(function(index) { return $(this).text() === " "; });
    

    you could also look at the contains selector documentation: https://api.jquery.com/contains-selector/

    0 讨论(0)
  • 2021-01-28 13:53
    var myRegEx = new RegExp('^ \s');    
    
    $('#myDiv h1').each(function() {
        var myText = $(this).text();
    
        if (myText.match(myRegEx) ) { ... }
    });
    
    0 讨论(0)
  • 2021-01-28 13:59

    If you want to remove all h1s that contains an nbsp you can try something like this: removing all elements that contains nbsp

    $("h1").each(function() {
    if ($(this).html().indexOf(" ") != -1) {
        $(this).remove();
    }
    });
    

    Now if you want to remove elements that matches exactly an nbsp just modify it as this: modified version

    $("h1").each(function() {
        if ($(this).html() === " ") {
            $(this).remove();
        }
    });
    
    0 讨论(0)
提交回复
热议问题