jQuery 选择包含链接的元素,该链接包含数组的任何元素

标签 jquery jquery-selectors

我正在尝试制定一个复杂的 jQuery 选择。我有一个数组,我需要选择包含链接 a.bardiv.foo ,并且该链接的 href 包含任何字符串在数组中。

例如:

<html>
<div class=foo>
  some text
  <a class=bar href=example.com?q=widget&id=23456789>search</a>
</div>
</html>

和 jScript

widgets[0]='12345678';
widgets[1]='23456789';
widgets[2]='34567890';

我想突出显示此 div,因为查询字符串包含 23456789,它是 widgets[] 中的元素之一。

谢谢

最佳答案

$('div.foo > a.bar[href*=23456789');

select all a which contains '23456789' in the href attributes with the bar class which are direct descendant of all div with a class foo.

for (num in widgets)
{
    $('div.foo > a.bar[href*=' + num + ']').doSomething();
}

关于jQuery 选择包含链接的元素,该链接包含数组的任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699072/

相关文章:

jquery 切换 Accordion 无需插件

php - 插入前检查数据库中是否存在Random

jquery - 检查 jQuery 中的所有复选框

jquery - 如何在中继器/网格控件内实现 Jquery 选项卡?

javascript - jQuery 选择器 : Why is $ ("#id"). 查找 ("p") 比 $ ("#id p"快)

javascript - jquery中的滚动绑定(bind)问题

javascript - 尝试使用 Javascript/Jquery 解析 XML 文件时出现错误

javascript - 选择国家时改变颜色

javascript - document.querySelector() 可以根据 SRC 文件名(而不是完整的 URL)找到 IMG 元素吗?

javascript - 如何在被单击的元素上执行 EventListener 脚本