具有nth-of-type()的jQuery选择器 [英] jQuery selector with nth-of-type()

查看:100
本文介绍了具有nth-of-type()的jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Chrome开发工具

Using Chrome Development Tools

jQuery('.proejct-text-field')

给了我四个HTML元素实例:

gives me four instances of HTML Elements:

<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>
<div class=".proejct-text-field ...">...<>

尝试以下操作不会返回任何元素.

Trying the following doesn't return any Elements.

jQuery('.proejct-text-field:nth-of-type(1)')

据我了解,应该返回第一个Element.我只是使用jQuery来找到适合我目的的正确选择器,并将它们带到我的CSS文件中.因此,我该如何选择这些Divs中的第一个Elment.顺便说一句:它们没有包装到某个父元素中.因此,任何子方法都行不通.此外,div的数量是可变的.

As for my understanding the first Element should be returned. I just use jQuery in order to find the right selector for my purposes and to take them into my css file. So How can I select the first Elment of those Divs. Btw: They are not wrapped into a certain parent element. So any child methods won't work. Further the number of divs is variable.

推荐答案

您的class名称不正确,您在 class=".proejct-text-field ..." 中具有..

Your class name is incorrect, you are having a . in the class=".proejct-text-field ...".

此外,您可以使用.first()选择第一个元素,例如

Also, you can use .first() to select the very first element like

$('.proejct-text-field').first();

您无法选择nth-of-class,因为无法使用class选择nth元素,因此您的选择器应该可以工作,尽管它不能工作,因为您必须从class您的DOM中的名称)*

You cannot select nth-of-class because there's no such way to select nth element with a class, so your selector should work though it doesn't because you have to take out the period . from the class name in your DOM)*

演示

演示2 (使用jQuery .first())

这篇关于具有nth-of-type()的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆