gpt4 book ai didi

javascript - 如何 - 将事件类添加到当前元素

转载 作者:行者123 更新时间:2023-12-05 05:49:33 25 4
gpt4 key购买 nike

我有一个侧边栏的 jsp 页面,我想在有人点击它时添加一个 Action 类。我正在按照 w3schools.com 的说明进行操作。链接 = https://www.w3schools.com/howto/howto_js_active_element.asp但我不确定如何更改它以匹配我的代码,因为我是一个 javascript 新手。有什么建议吗?


<body>
<div class="col-md-3" id="DIV">
<ul class="nav nav-pills nav-stacked hidden-sm hidden-xs">
<li role="presentation" class="active"><a href="${webHome}/home">Dashboard</a></li>
<li role="presentation"><a href="${webHome}/task">Task List</a></li>
<li role="presentation" >
<a href="${webHome}/app?domainId=${userSession.domainId}">Applications</a>
</li>
</ul>
</div>

<script>
// Add active class to the current button (highlight it)
var header = document.getElementById("DIV");
var btns = header.getElementsByTagName("presentation");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
</script>
</body>

最佳答案

替换

var btns = header.getElementsByTagName("presentation");

const btns = header.querySelectorAll("li[role=presentation]");

https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll

关于javascript - 如何 - 将事件类添加到当前元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70652352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com