jQuery 中有一个选择器——:contains
例子:
str = 'test';
blocked_div = $(".mydiv:contains("+str+")");
blocked_div.find('img').hide();
...
<a href="http://example.com">test</a>
...
据我了解,它是在“.mydiv”元素内搜索“测试”文本。 但是现在我需要在“.mydiv”元素中找到一个链接“http://example.com”,并且 :contains 在这种情况下不起作用。你能推荐一个解决方案吗?
最佳答案
你的意思是:
$(".mydiv a[href='example.com']");
您还可以使用 *=
或 ^=
过滤器分别查找在任意位置或字符串开头包含指定文本的链接。
// find links containing "example" in href at any place
$(".mydiv a[href*='example']");
对于您的 str
变量,您可以像这样修改它:
$('.mydiv a[href=' + str + ']'); // exact match
$('.mydiv a[href*=' + str + ']'); // contains str anywhere in href
关于javascript - jQuery ":contains"替代链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10836645/