表面上看,这应该很容易,但是我已经偶然发现了一段时间。
我试图在出现某些文本时隐藏仅<h2>
标记。 CMS生成标签。 HTML看起来像这样:
<div class="Block Moveable Panel" id="BrandContent">
<h2>All Brands</h2>
<div class="BlockContent">
BRANDS LISTED HERE...
</div>
</div>
如果h2标签中包含“所有品牌”,那么我希望标签被删除或隐藏。
我尝试了以下代码的一些组合,但未成功:
if($('#BrandContent > div.h2:contains("All Brands")').length > 0) {
$('#BrandContent h2').css('visibility', 'hidden')
}
和
$('#BrandContent h2').remove(":contains('All Brands')");
预先感谢您的任何建议。
中号
最佳答案
试试这个选择器
$('#BrandContent > h2:contains("All Brands")').css('visibility', 'hidden')
Check Fiddle
您使用的是id属性,但使用的是类选择器
id="BrandContent">
您的第一次尝试有一个小错误
$('#BrandContent > div.h2
您正在寻找的
div
类别.h2
在您的HTML
中不存在