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