簡體   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