使用 Chrome 开发工具
jQuery('.proejct-text-field')
给我四个 HTML 元素实例:
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
尝试以下操作不会返回任何元素。
jQuery('.proejct-text-field:nth-of-type(1)')
据我了解,应该返回第一个元素。我只是使用 jQuery 来找到适合我的目的的正确选择器并将它们放入我的 css 文件中。那么我怎样才能选择这些 Div 的第一个元素呢?顺便说一句:它们没有包装到某个父元素中。所以任何子方法都不起作用。此外,div 的数量是可变的。
最佳答案
nth-of-type selector查找元素类型,例如 div、span
等,而不查找类名称或任何其他选择器
jQuery('.proejct-text-field:eq(0)')
jQuery('.proejct-text-field').eq(0)
关于带有 nth-of-type() 的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782590/