简体   繁体   English

如何使用JQuery选择容器元素内的第一个div元素?

[英]How can I use JQuery to select the first div element inside a container element?

I am pretty new in JQuery and I have the following situation: 我在JQuery中还很陌生,并且遇到以下情况:

<!-- Bottone relativo ai progetti WIFI: -->
<a title="WIFI" href="javascript: void(0)" id="showWifi_${item.index}" class="showWifi">
    <div class="news_box news_box_01 hvr-underline-from-center " style="margin-right: 50px;"></div>
</a>

I have the a tag having class="showWifi" . 我有a具有class="showWifi" a标签。

I have to use JQuery to select the first div element inside this a tag having class="showWifi" . 我必须使用JQuery在a具有class="showWifi" a标签中选择第一个div元素。

I don't want set an id to this tag, but I want use the JQuery syntax to select the first div tag 我不想为此标签设置id ,但是我想使用JQuery语法选择第一个div标签

How can I do this? 我怎样才能做到这一点?

You can use first() 您可以使用first()

$('a.showWifi div').first().addClass('something')

You can also use :first pseudo-selector 您也可以使用:first伪选择器

$('a.showWifi div:first').addClass('something')

Note: first() is faster than :first (Thanks to @Zeratops). 注意: first():first更快(感谢@Zeratops)。 See: jQuery :first vs. .first() 参见: jQuery:first与.first()

You can use :first 您可以使用:first

Selects the first matched element. 选择第一个匹配的元素。

$('a.showWifi > div:first')

Use .first() 使用.first()

$(".showWifi").find("div").first();

Working JSFIDDLE: http://jsfiddle.net/uamkvdkr/ 工作的JSFIDDLE: http//jsfiddle.net/uamkvdkr/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM