我希望能够根据 HTML 内容禁用链接。有许多可用的分类部分,但并不总是填写。我希望链接变灰,以便最终观众知道该分类没有内容。我的 HTML 结构如下所示:
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<p><a class="fancyTxt" href="Classifieds/050.html">050 Farms For Rent</a></p>
</div><!--"col-xs-12 col-sm-6 col-md-4 col-lg-4-->
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<p><a class="fancyTxt" href="Classifieds/051.html">051 Houses For Rent</a></p>
</div><!--"col-xs-12 col-sm-6 col-md-4 col-lg-4-->
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<p><a class="fancyTxt" href="Classifieds/052.html">052 Miscellaneous</a></p>
</div><!--"col-xs-12 col-sm-6 col-md-4 col-lg-4-->
每种类型的分类都有一个 HTML 文件,因为这是我们的分类程序输出的内容。如果分类中没有任何内容,它会放置一串文本作为页脚,但此文本也在每个分类 HTML 文件中。这是正文:
<p>www.domainname.com<BR>
Your source for local online<BR>
classifieds!</p>
当该文本是分类中显示的唯一文本时,我想搜索该文本以禁用链接。
谢谢!
编辑:包含内容的分类页面示例。
<!-- Classification Title Here -->
004 Announcements
<!-- Begin output Ad Text <startTags> </startTags> -->
<p><FONT SIZE=3>text about classified here</FONT></p><BR><HR>
<p><FONT SIZE=3>Text about classified here </FONT></p><BR><HR>
<p><FONT SIZE=3><DIV ALIGN=CENTER>www.domainname.com<BR>
Your source for local online<BR>
classifieds!</DIV></FONT></p><BR><HR>
<!-- End output Ad Text <endTags><BR><HR></endTags> -->
最佳答案
由于您的问题不是禁用链接,而是找到需要禁用的容器,也许可以尝试类似的方法:
$('.col-xs-12.col-sm-6.col-md-4.col-lg-4').each(function() {
var p = $(this).children('p');
if (p.length === 1 && p.text().match(/^www\.domainname\.com/i) !== null) {
// disable link within $(this)
}
});
这会获取所有子段落元素并确保只有一个且其内容以“页脚”内容开头。
关于javascript - 用于根据其 html 内容禁用链接的 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535342/