簡體   English   中英

使用javascript或jQuery隱藏所有帶有text或innerHTML的'a'元素,這些元素匹配數字'0'或自定義值

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

我需要隱藏所有帶有text或innerHTML <a>元素,這些元素與數字'foo'匹配,或者使用javascript或jQuery隱藏自定義值。

<li><a href="#" class="dir">foo</a></li>

我努力了

jQuery(document).ready(function() {
    if (jquery().text().html("foo"){
        ('li >a').fadeOut()
    }
});
$('a:contains(foo)').hide();

完成。

要么:

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

使用后面的選項,您可以更多地定制它,例如:

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

一種方法,假設您正在搜索的文本正是您使用的字符串, 無恥地偷偷 Jonathan Sampson 致敬

創建:exactly選擇器:

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

像這樣使用:

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

參考文獻:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM