繁体   English   中英

如何在$(this)JQuery选择器选择的元素内选择第一个div?

[英]How to select the first div inside the element selected by the $(this) JQuery selector?

进入JSP页面,我会遇到以下情况:

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

我在JQuery中还很陌生,并且遇到以下情况:

如您所见,我有一个标记为id =“ showWifi _ $ {item.index}” 标签,其中item.index是由更多外部迭代生成的值。

在此页面中,我具有以下JQuery脚本:

$(".showWifi").click(function(event){

    clickedButton = this.id;      

    splitButtonId = clickedButton.split("_"); 

    idToShow = "progettiWifi_" + splitButtonId[1];
    idToHide = "progettiPnsd_" + splitButtonId[1];

    document.getElementById(idToShow).style.display = 'block';
    document.getElementById(idToHide).style.display = 'none';

    //$(this).addClass("highLightButton");

    $('a.showWifi div').first().addClass('highLightButton');
    $('a.showPnsd div').first().removeClass('highLightButton');


});

最后两行选择具有showWifi作为类标签内的第一个div。 问题是我必须使用代表被单击元素的** $(this)元素内的第一个div替换这些行。

我该怎么办?

如果<a>内有多个divs ,则可以使用以下代码获取第一个div。

$(this).find('div').first().addClass('highLightButton');

请检查以下解决方案: 如何使用JQuery选择容器元素内的第一个div元素?

由于.showWifi只有一个div, .showWifi不需要first()

 $(this).find('div').addClass('highLightButton');

尝试这个:

$(this).children('div:first').addClass('highLightButton');
$(this).children('div:first').removeClass('highLightButton');

更多信息: https//api.jquery.com/first-selector/

暂无
暂无

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

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