我试图找到一个document.querySelector的元素,它有multiple data-attributes:

<div class="text-right" data-point-id="7febe088-4eca-493b-8455-385b39ad30e3" data-period="current">-</div>

我想到了这样的事情:

document.querySelector('[data-point-id="7febe088-4eca-493b-8455-385b39ad30e3"] [data-period="current"]')

但它不起作用

<div class="text-right" data-point-id="7febe088-4eca-493b-8455-385b39ad30e3"> <span data-period="current">-</span> </div>

那么,有没有搜索both attributes at once的选项

推荐答案

There should not be a space between the 2 selectors

document.querySelector('[data-point-id="7febe088-4eca-493b-8455-385b39ad30e3"][data-period="current"]')

if you give a space between them it will become a descendant selector, ie it will search for an element attribute data-period="current" which is inside an element with data-point-id="7febe088-4eca-493b-8455-385b39ad30e3" like

<div class="text-right" data-point-id="7febe088-4eca-493b-8455-385b39ad30e3">
    <div data-period="current">-</div>
</div>

Jquery相关问答推荐

jQuery .first() 方法返回第一个元素的集合

用 jQuery/JavaScript 检测数字或字母?

提高 jQuery Select 器性能的好方法?

JSLint 错误:将调用移动到包含函数的括号中

jQuery 的元素或类 LIKE Select 器?

jQuery - 向下滚动时缩小的粘性标题

Google Maps API v3 infowindow 关闭事件/回调?

使用 bootstrap-datepicker 检测对选定日期的更改

如何使用 jQuery 获取 div 的当前类?

可以在删除类时反转css动画吗?

如何在 jQuery .each() 的每次迭代之间添加暂停?

blueimp 文件上传插件中的 maxFileSize 和 acceptFileTypes 不起作用.为什么?

Isotope 和 Masonry jQuery 插件之间的区别

!!~(不是波浪号/bang bang 波浪号)如何改变包含/包含数组方法调用的结果?

bootstrap 模式:关闭当前,打开新

在jQuery中获取CSS规则的百分比值

如何删除集中的 contenteditable pre 周围的边框?

jQuery .each() 索引?

jQuery,简单的轮询示例

Drop 事件未在 chrome 中触发