通过 href 的文本找到 <a> 标签

find the <a> tag by text of the href

我想用 jQuery 找到一个 link 并隐藏它。我想使用 jQuery 搜索 link 的文本。这不起作用:

<a href="example.com/foo-bar">a website link</a>
function replace_text() {
  var linkText = jQuery('a').text();
  var answer = linkText.replace('a website link', '');
}
replace_text();

要通过文本隐藏元素,您可以使用 :contains 选择器,然后是 hide(),如下所示:

$('a:contains("a website link")').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="example.com/foo-bar">a website link</a>

注意上面是不区分大小写的贪心匹配。如果你想要 精确 匹配,你可以使用 filter(),像这样:

$('a').filter(function() {
  return $(this).text().trim() == 'a website link';
}).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="example.com/foo-bar">a website link</a>