Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用 id 选择器:
$("#id")
有没有办法只对页面上具有该 ID 的第 n 个元素执行此操作?IE
$("#id:n")
页面中只能有一个具有给定 id 的元素。
从HTML 规范:
文档中不能有多个元素具有相同的 id 值。
现在假设您想在页面中获取具有给定类的第 n 个元素,您可以使用eq:
$('.myclass').eq(index)
你可以这样做:
$("#id:eq(n)")
但就像@dystroy 的回答一样,它应该只有一个页面中的 1 个 id,所以你最好使用类。
您可以使用:eq(n)选择器获取第 n 个项目,但 id 应该是唯一的。
:eq(n)
您应该使用该class属性对相似的元素进行分组。
class