抱歉,如果这看起来是一个愚蠢的菜鸟问题,但我不明白为什么这个页面在 Chrome 中加载后无需单击鼠标即可进入新页面。我正在尝试点击谷歌链接来获取另一个页面...谢谢!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>3 JS</h1>
<a id="googlelink" href="https://www.google.com">Google</a>
<hr>
<a href="https://www.google.com">Google</a>
<hr>
</body>
</html>
<script>
document.getElementById("googlelink").addEventListener("click", redirectToYahoo());
document.getElementById("googlelink").addEventListener("auxclick", redirectToYahooNewTab());
function redirectToYahoo() {
window.location.href = "https://www.yahoo.com";
}
function redirectToYahooNewTab() {
window.open('https://www.yahoo.com', '_blank');
}
</script>
最佳答案
看起来,在您的 addEventListener 调用中,您正在执行重定向函数,而不是将其作为参数传递给 addEventListener。应该是:
addEventListener("click", redirectToYahoo);
不带括号以避免在页面加载期间调用它。
关于javascript - 重定向链接而不点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730930/