我想把一个函数传递给我的a标签链接的href属性,但无论我尝试什么都不起作用。它要么导致404页面,要么根本无法点击。我做错了什么?
这个是不可点击的
<body>
<a id="specialLink" href="#" onClick="redirectToApp()">点击我</a>
<script>
document.getElementById('specialLink').href = redirectToApp();
function redirectToApp () {
return "http://www.google.com";
}
</script>
</body>
这个是不可点击的
<body>
<a href="#" onClick="document.location.href=redirectToApp()">点击我</a>
<script>
function redirectToApp () {
return "http://www.google.com";
}
</script>
</body>
这个导致了一个404页面
<body>
<a href="javascript:document.location.href=redirectToApp();">点击我</a>
<script>
function redirectToApp () {
return "http://www.google.com";
}
</script>
</body>
我可能还应该说明一下,我是通过电子邮件发送这个HTML,链接应该在其中打开,除非我只是将URL作为字符串传递给href,否则什么都不起作用。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你可以尝试这样做:
const anchor = document.getElementById('specialLink') anchor.addEventListener((e) => { e.preventDefault() window.open("https://google.com"); })并且移除锚点标签上的
onClick属性