簡體   English   中英

使用JS將類應用於父元素

[英]Applying classes to a parent element with JS

當用戶點擊鏈接時,我需要將一個類應用於包含錨標記的<li>標記。 像這樣:

<ul>
<li><a href="#">Just an Example</a></li>
</ul>

因此,當用戶點擊<a href="#">Just an Example</a> ,我需要將一個類應用於包含它的<li> 我該如何定位? 我正在使用jquery。

$('a[href=#]').click(function(){
   $(this).closest('li').addClass('your_class_name');
});

如果它保證這樣一個錨的父元素是LI你也可以使用

$(this).parent().addClass('your_class_name');
$("a").click(function() {
   $(this).parent().whateveryouwant();
}

只是用

$('a').click(function(){
    $(this).parent().addClass('theClass');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM