簡體   English   中英

我如何隱藏.each()Jquery之外的特定元素

[英]How can I hide a specific element that is outside of .each() Jquery

通過Sharepoint,可以生成一堆tds,其中包含一些元素。 因此,為了清楚起見,我無法編輯或更改元素,因為它已生成。

我要完成的工作是遍歷所有'.js-contentFollowing-itemLink' ,然后如果.text()包含我要查找的特定文本,則'<span> Stop following</span>'應該與'.hide()'

我似乎無法做到這一點,我嘗試了許多方法。

這是jsfiddle: http : //jsfiddle.net/QXwyk/3/

還要注意,我無法獲取唯一的ID,並且其中許多元素是用不同的值和文本生成的。

HTML:

<span class="ms-contentFollowing-itemTitle">
<a href="#" class="js-contentFollowing-itemLink">test</a>
</span>

<br>
<div class="js-contentFollowing-itemUrlDiv">
<span class="ms-metadata ms-contentFollowing-itemUrl"></span>

    <input type="text" readonly="readonly" class="ms-metadata ms-contentFollowing-itemUrl ms-contentFollowing-itemFullUrl" style="visibility: visible; border-color: transparent; background-color: transparent;">
</div>
<a href="#" class="ms-secondaryCommandLink"><span>Stop following</span></a>

我的JS:

$('.js-contentFollowing-itemLink').each(function () {
        if ($(this).text() == "test")

            $(this).closest("span").hide();


    });

注意:我知道它隱藏了錯誤的元素,但是If語句在無法完成的if語句中運行其代碼,因此我需要隱藏“停止關注”。 該JS只是我做過的不起作用的示例之一。

我試過$('.ms-secondaryCommandLink span').hide(); 在if語句中,但是用“ Stop following”刪除了所有<span> :/

謝謝一群!

您的代碼中的幾個問題:

$('.js-contentFollowing-itemLink').each(function () {
    if ($(this).text() == "test")
    var text = $(this).parent().parent().parent().hide); <-- hide not closed properly
} <--- Extra bracket here..
});

工作代碼:

$('.js-contentFollowing-itemLink').each(function () {
    if ($(this).text() == "test") {
        var text = $(this).parent().parent().parent().hide();
    }
});

HTML

<div class="ms-content">
<span class="ms-contentFollowing-itemTitle">
<a href="#" class="js-contentFollowing-itemLink">test</a>
</span>

<br>
<div class="js-contentFollowing-itemUrlDiv" id="contentFollowingUrl_18" url="http://dev/socialsites/Utförsåkning">
<span class="ms-metadata ms-contentFollowing-itemUrl"></span>

    <input type="text" readonly="readonly" class="ms-metadata ms-contentFollowing-itemUrl ms-contentFollowing-itemFullUrl" style="visibility: visible; border-color: transparent; background-color: transparent;">
</div>
<a href="#" class="ms-secondaryCommandLink"><span>Stop following</span></a>
</div>

JS

$('.js-contentFollowing-itemLink').each(function () {
     if ($(this).text() == "test")
         var text = $(this).closest('.ms-content').find('.ms-secondaryCommandLink').hide();
});

http://jsfiddle.net/QXwyk/4/

您可以通過以下方式選擇包含特定文本的對象,根據該對象可以執行其余操作...

示例:$('。js-contentFollowing-itemLink:contains(test)')。hide(); 將隱藏“測試”

暫無
暫無

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

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