进入 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 还很陌生,我有以下情况:
如您所见,我的 a 标签具有 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');
});
最后 2 行选择 a 标记内的第一个 div,其类为 showWifi。问题是我必须使用代表单击元素的 **$(this) 元素内的第一个 div 替换这些行。
我该如何做这件事?
最佳答案
如果您有多个divs
里面<a>
,您可以使用以下代码来获取第一个div。
$(this).find('div').first().addClass('highLightButton');
请检查此解决方案:How can I use JQuery to select the first div element inside a container element?
关于javascript - 如何选择 $(this) JQuery 选择器选择的元素内的第一个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500422/