简体   繁体   English

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

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

into a JSP page I have something like this: 进入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>

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

As you can see I have the a tag having id="showWifi_${item.index}" where the item.index is a value generated by a more external iteration. 如您所见,我有一个标记为id =“ showWifi _ $ {item.index}” 标签,其中item.index是由更多外部迭代生成的值。

In this page I have this JQuery script: 在此页面中,我具有以下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');


});

The last 2 rows select the first div inside the a tag having showWifi as class. 最后两行选择具有showWifi作为类标签内的第一个div。 The problem is that I have to replace these lines using the first div inside the **$(this) element that represent the clicked element. 问题是我必须使用代表被单击元素的** $(this)元素内的第一个div替换这些行。

How can I do this thing? 我该怎么办?

In case you have more than one divs inside <a> , you can use the following code to get the first div. 如果<a>内有多个divs ,则可以使用以下代码获取第一个div。

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

Please check this solution: How can I use JQuery to select the first div element inside a container element? 请检查以下解决方案: 如何使用JQuery选择容器元素内的第一个div元素?

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

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

Try this: 尝试这个:

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

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

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

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