如果href为空,我如何隐藏链接文本?

4
我可以帮助您进行翻译。以下是您需要翻译的内容:

我有一系列链接被生成,其中一些具有空的href。 我想使用Jquery和/或CSS将空白链接隐藏起来,甚至可能出现#。

链接的样式如下:

<a class="Highlighted" id="hyperlinkViewProfile" href="">text to hide</a>

当你说“生成链接”时,你是什么意思? 如果你正在使用jQuery生成链接,那么在你生成的地方,你可以添加$(this).attr("href", "#"); - Ani
3个回答

11
使用CSS:
A[href=""], A[href="#"], A:not([href]) {
  display: none;
}

这适用于没有href或href为"#"的链接。如果你想匹配href中任何位置有#的链接,请使用A[href~="#"]
在旧浏览器中未经测试,如果是这种情况,你可以使用jQuery选择器。

2

DEMO

可以翻译为:

{{链接1:演示}}

$('a').filter(function(){return $(this).attr('href') === "" || $(this).attr('href') === "#"}).hide();

1
一个简单的jQuery:

$("a[href=''], a:not([href])").hide(); 

可以修改选择器,使其包括 # 链接:

$("a[href=''], a:not([href]), a:[href='#']").hide(); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接