如何使用JQuery来选择容器元素中的第一个div元素? [英] How can I use JQuery to select the first div element inside a container element?

查看:905
本文介绍了如何使用JQuery来选择容器元素中的第一个div元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JQuery中很新,我有以下情况:

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

我有 a 标签有 class =showWifi



我必须使用JQuery来选择第一个 div 元素 a 标签中有 class =showWifi



我不想为此标签设置 id ,但我想使用JQuery语法选择第一个 div 标签



我该如何做?

解决方案

您可以使用 first()

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

您还可以使用 :first 伪选择者

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

注意: first():first (感谢@Zeratops)。请参阅: jQuery:first vs. .first()


I am pretty new in JQuery and I have the following situation:

<!-- 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".

I have to use JQuery to select the first div element inside this a tag having class="showWifi".

I don't want set an id to this tag, but I want use the JQuery syntax to select the first div tag

How can I do this?

解决方案

You can use first()

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

You can also use :first pseudo-selector

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

Note: first() is faster than :first(Thanks to @Zeratops). See: jQuery :first vs. .first()

这篇关于如何使用JQuery来选择容器元素中的第一个div元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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