admin管理员组文章数量:1410705
I have the following :
<div id="first">
<div id="second">
<button id="third"></button>
</div>
</div>
The DIV with ID of first
will never change it's ID but second
and third
might change.
If it never changed I'd use something the following to target third
:
submission = document.getElementById("third");
However, using first
, how would I access it now? I've tried the following but get a different result back than the above (which is what I need)
submission = document.querySelectorAll("div#first div button");
This seems to work but doesn't look clean :
document.getElementById("first").children[0].children[0];
I have the following :
<div id="first">
<div id="second">
<button id="third"></button>
</div>
</div>
The DIV with ID of first
will never change it's ID but second
and third
might change.
If it never changed I'd use something the following to target third
:
submission = document.getElementById("third");
However, using first
, how would I access it now? I've tried the following but get a different result back than the above (which is what I need)
submission = document.querySelectorAll("div#first div button");
This seems to work but doesn't look clean :
document.getElementById("first").children[0].children[0];
Share
Improve this question
edited Sep 5, 2017 at 14:25
Zakaria Acharki
67.5k15 gold badges78 silver badges106 bronze badges
asked Sep 5, 2017 at 14:12
pee2peepee2pee
3,78211 gold badges65 silver badges147 bronze badges
4
- In your examples you are using vanilla JS, but you tagged it as jquery? – Thomas Gak-Deluen Commented Sep 5, 2017 at 14:13
-
In what way might the "
second
andthird
...change"? – David Thomas Commented Sep 5, 2017 at 14:13 - They might be any other value – pee2pee Commented Sep 5, 2017 at 14:16
- 1 @tgdn sorry, I hope you can forgive me – pee2pee Commented Sep 5, 2017 at 14:20
2 Answers
Reset to default 3You should use querySelector()
if you want to get the button
element instead of querySelectorAll()
that will return a set of elements that fits the selector :
submission = document.querySelector("div#first div button");
Hope this helps.
console.log( document.getElementById("third") );
console.log( document.querySelector("div#first div button") );
<div id="first">
<div id="second">
<button id="third"></button>
</div>
</div>
var btn = document.querySelectorAll('#first> div> button');
var btn1 = document.querySelector('#first> div> button');
console.dir(btn)
console.dir(btn1)
<div id="first">
<div id="second">
<button id="third"></button>
</div>
</div>
If you do document.querySelectorAll
then this will return Array
of NodeLists
and If you do document.querySelector
then it will return the specific element
var btn = document.querySelectorAll('#first> div> button');
var btn1 = document.querySelector('#first> div> button');
console.dir(btn)
console.dir(btn1)
本文标签: javascriptGet the grandchild of an element with specific IDStack Overflow
版权声明:本文标题:javascript - Get the grandchild of an element with specific ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744865578a2629324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论