jquery - 如何在 jQuery 中从父 ID 中选择子 ID?

标签 jquery html

这里我有父 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/

相关文章:

html - 响应式网格是什么以及如何实现它

jquery - Bootstrap 3 的分割按钮插入符高度小于主按钮

jquery - jCarousel 卸载/禁用/删除

jquery - CSS3 背景不打印

javascript - jQuery - 使用相同的数据更新 2 个或更多表单字段

javascript - 获取包含特定值的 <li> 标签的 id

javascript - 使用 JavaScript jQuery 遍历表格行

Python (Selenium) - 使用 Xpath 时选择器无效

HTML/CSS : How do i make images from my website appear in other websites?

html - 如何禁用 FavIcon.ico