隐藏一个div';当不同的div包含带有jquery的特定文本时
我有以下三个分区:隐藏一个div';当不同的div包含带有jquery的特定文本时,jquery,html,Jquery,Html,我有以下三个分区: <div class="price"><span>FREE</span> <div class="transaction-type">FOR SELL</div></div><br/> <div class="price"><span>$250 USD</span> <div class="transaction-type">FOR SELL&
<div class="price"><span>FREE</span> <div class="transaction-type">FOR SELL</div></div><br/>
<div class="price"><span>$250 USD</span> <div class="transaction-type">FOR SELL</div></div><br/>
<div class="price"><span>$800 USD</span> <div class="transaction-type">FOR SELL</div></div>
我的问题是,我的代码还隐藏了所有其他包含与“FREE”
word不同内容的跨距
您可以选中my以循环元素,并使用
$(this)
引用正在循环的元素。然后使用:
$('.price>span').each(function() {
if ($(this).text().indexOf('FREE') >= 0) $(this).next('.transaction-type').hide();
})
$('.price>span')。每个(函数(){
if($(this.text().indexOf('FREE')>=0)$(this.next('.transaction type').hide();
})
自由的
出售
250美元
出售
800美元
出售
您希望在元素上循环,并使用$(this)
引用正在循环的元素。然后使用:
$('.price>span').each(function() {
if ($(this).text().indexOf('FREE') >= 0) $(this).next('.transaction-type').hide();
})
$('.price>span')。每个(函数(){
if($(this.text().indexOf('FREE')>=0)$(this.next('.transaction type').hide();
})
自由的
出售
250美元
出售
800美元
出售
可以使用选择器和或以特定实例为目标
$('.price>span:contains(“FREE”))。同级('.transaction type')。hide()
免费出售
出售250美元
售价800美元
可以使用选择器和或来针对特定实例
$('.price>span:contains(“FREE”))。同级('.transaction type')。hide()
免费出售
出售250美元
售价800美元
如果您使用contains
,那么最后仍然会有一个jQuery对象,您可以使用hide()
将其隐藏:
如果使用
contains
,那么最后仍然会有一个jQuery对象,您可以使用hide()
将其隐藏:
您可以使用模式检查区分大小写
$('.price > span').each(function(){
if ($(this).text()==="FREE"){
$(this).next().hide();
}
})
您可以使用模式检查区分大小写
$('.price > span').each(function(){
if ($(this).text()==="FREE"){
$(this).next().hide();
}
})
循环
span
s并查找您当前span的兄弟姐妹和$(此)。兄弟姐妹('.transaction type')
。循环span
s并查找您当前span
的兄弟姐妹和$(此)。兄弟姐妹('.transaction type')
。谢谢@j08691。它工作得很好。每次学习新东西我都很兴奋。谢谢@j08691。它工作得很好。每次我学到新东西,我都感到很兴奋。