document.querySelector multiple data-attributes in one element

后端 未结 2 1558
夕颜
夕颜 2020-12-09 07:26

I\'m trying to find an element with document.querySelector which has multiple data-attributes:

相关标签:
2条回答
  • 2020-12-09 07:52

    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>
    
    0 讨论(0)
  • 2020-12-09 08:05

    space in selector looks for [data-period="current"] in[data-point-id="7febe088-4eca-493b-8455-385b39ad30e3"] .You don't need to put space in between attribute value selector:

    document.querySelector('[data-point-id="7febe088-4eca-493b-8455-385b39ad30e3"][data-period="current"]')
    
    0 讨论(0)
提交回复
热议问题