1

On the face of it, this should be easy but I have stumbled on it for some time already.

I am trying to hide a <h2> only tag when certain text appears. CMS generates the tag. HTML looks like this:

<div class="Block Moveable Panel" id="BrandContent">
  <h2>All Brands</h2>
  <div class="BlockContent">
     BRANDS LISTED HERE...
  </div>
</div>

If "All Brands" is included in the h2 tag, then I want the tag removed or hidden.

I have tried a few combinations of the below code without success:

if($('#BrandContent > div.h2:contains("All Brands")').length > 0) {
    $('#BrandContent h2').css('visibility', 'hidden')
}

and

 $('#BrandContent h2').remove(":contains('All Brands')");

Thank you in advance for any suggestions. M

4

4 回答 4

1

试试这个选择器

$('#BrandContent > h2:contains("All Brands")').css('visibility', 'hidden')

检查小提琴

您使用的是 id 属性,但使用的是类选择器

id="BrandContent">

你的第一次尝试有一个小错误

$('#BrandContent > div.h2

您正在寻找一个没有出现在您的div课程中的课程.h2HTML

于 2013-08-07T05:18:08.000 回答
1

您的选择器错误 -

应该是这样的——

$('#BrandContent > h2:contains("All Brands")')
于 2013-08-07T05:19:15.847 回答
1

尝试

$('#BrandContent > h2:contains("All Brands"))
于 2013-08-07T05:19:53.870 回答
0

您使用了错误的选择器,因为“BrandContent”是 id 并且您正在用作类。所以它应该如下所示:

$('#BrandContent > h2:contains("All Brands")').hide();  

JSFIDDLE 演示

于 2013-08-07T05:20:20.477 回答