gpt4 book ai didi

javascript - 将点击事件附加到 div 内的某些链接

转载 作者:行者123 更新时间:2023-11-28 03:46:26 25 4
gpt4 key购买 nike

最佳答案

使用Element.matches()仅处理您想要的 a 标签:

var navFly = document.querySelector('#navfly');

navFly.addEventListener('click', function(e) {
// if it's not an a tag, that doesn't start with http do nothing
if (!e.target.matches('a:not([href^="http"])')) {
return;
}
e.preventDefault();

var newpg = e.target.getAttribute('href');

console.log(newpg);
// take href value, modify url for content location
// fetch content with ajax GET
// insert within content div
});
<div id="navfly">
<a href="content-one.htm">Content 1</a><br>
<a href="content-two.htm">Content 2</a><br>
<a href="content-three.htm">Content 3</a><br>
<a href="content-four.htm">Content 4</a><br>
<a href="https://some.otherdomain.com">External wepage</a><br>
</div>

关于javascript - 将点击事件附加到 div 内的某些链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48440214/

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