这里我有父 div (div1) ,通过这个 id 我想选择子 id (children2) 。
注意:-我不想在代码中指定为children2..它应该是动态的
HTML:-
<div id='div1'>
<div id='children1'>
<input></input>
<input></input>
</div>
<div id='children2'>
<input></input>
<input></input>
</div>
</div>
最佳答案
如果 ID 是静态的,您可以简单地使用 id 选择器来定位元素:
$('#children2')
如果 Id 不唯一,那么您可以在 #div1
中找到直接 div 元素以及 :eq(1)
来定位匹配 dom 中的第二个元素:
$('#div1 > div:eq(1)').attr('id')
关于jquery - 如何在 jQuery 中从父 ID 中选择子 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910281/