[英]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();
});
您可以通过以下方式选择包含特定文本的对象,根据该对象可以执行其余操作...
示例:$('。js-contentFollowing-itemLink:contains(test)')。hide(); 将隐藏“测试”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.