Hide all 'a' elements with text or innerHTML that matches the number '0' or a custom value using javascript or jQuery

后端 未结 2 1962
梦如初夏
梦如初夏 2021-02-06 17:00
相关标签:
2条回答
  • 2021-02-06 17:36
    $('a:contains(foo)').hide();
    

    Done.

    Or:

    var customValue = "foo"
    $('a').filter(function(){
        return this.innerHTML === customValue;
    }).fadeOut();
    

    With the later option you custom it a lot more, like:

    var customValue = "foo"
    $('a').filter(function(){
        return this.innerHTML === customValue &&
               $(this).closest('div').length;
    }).fadeOut();
    
    0 讨论(0)
  • 2021-02-06 17:58

    One approach, assuming the text you're searching for is exactly the string you use, shamelessly stealing from paying homage to Jonathan Sampson:

    Creating the :exactly selector:

    $.extend($.expr[":"], {
        exactly: function( element, index, details, collection ){
            return $(element).text() === details[3];
        }
    });
    

    Used like so:

    $('a:exactly("foo")').fadeOut();
    

    References:

    • Jonathan Sampson:
      • The blog entry for :exactly() selector: http://sampsonblog.com/279/creating-your-own-custom-jquery-filters
      • The Stack Overflow question that prompted the creation of :exactly(): Is there any selector to do a perfect match against text?
    0 讨论(0)
提交回复
热议问题