0
我有一個包含鏈接的導航控件。類沒有被點擊切換
當用戶點擊鏈接時,我想切換兩個鏈接的類屬性。
我想爲點擊鏈接指定一類「定向」。
我也想從前面選定的鏈接中移除'目標'類。
這裏是我目前的es6 js。
$(() => {
//when one is clicked, remove the class from each of them and then add the class to the one that was clicked
$(document).on("click", ".tools-cover .tools-container > .row > .col-xs-12 > nav ul li a", (e) => {
$(document).find(".tools-cover .tools-container > .row > .col-xs-12 > nav ul li a").removeClass("targeted");
$(this).toggleClass("targeted");
});
//when the page has loaded, click the first nav link on the nav
$(document).find(".tools-cover .tools-container > .row > .col-xs-12 > nav ul li:first-child a").click();
});
<div class="tools-cover">
<div class="container tools-container">
<div class="row">
<div class="col-xs-12">
<nav>
<ul>
<li><a href="#">Feeds</a>
</li>
<li><a href="#">Wearisma links</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
分享html代碼以及 –
完蛋了在後了。 – SkullDev
恐怕在箭頭函數中的'this'關鍵字是不同的..不是你期望的元素 – aprok