문제

I have the html tag as,

<div data-id='test'></div>

I need to get this element using

document.querySelectorAll('[data-id="test"]');

But the above code returns always empty array. Please help me in getting the element using querySelectorAll().

도움이 되었습니까?

해결책

There shouldn't be any double quotes with your ID

document.querySelectorAll('[data-id=test]');

OR

document.querySelectorAll('div[data-id=test]');

And make sure you place this inside onload or make the JavaScript tag as defer="defer"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top