我有下面的html。当我点击IE中的链接时,它会将返回值转储到一个空页面上。在chrome中,它只是在不接触页面的情况下运行函数。在这方面,可以做些什么让IE表现得像chrome吗?
<html>
<body>
<script type="text/javascript">
function foo(){
return true;
}
</script>
<a title="call foo" href="javascript:foo()">return true</a>
</body>
</html>发布于 2019-03-06 18:13:30
javascript:方案URL的要点是它运行一些代码,这些代码生成一些数据,然后浏览器导航到这些数据。
如果你只是想在点击的时候运行一些JS :不要使用链接。
使用合适的工具来完成工作。
function foo(){
return true;
}
document.querySelector("button").addEventListener("click", foo);<button title="call foo">return true</a>
发布于 2019-03-06 18:18:00
<html>
<body>
<script type="text/javascript">
function foo(){
event.preventDefault();
return true;
}
</script>
<a title="call foo" href="javascript:foo()">return true</a>
</body>
</html>发布于 2019-03-07 15:49:06
您可以使用event.preventDefault()方法来防止单击事件中的超链接默认事件。如下所示:
<script type="text/javascript">
function foo(e) {
event.preventDefault();
////get the href attribute.
//var href = document.getElementById("btn").getAttribute("href");
////display the page. You could also using "window.open(href)" to open a new tab to display the page.
//window.location = href;
return true;
}
</script>
<a title="call foo" id="btn" href="https://www.google.com/" onclick="foo(this);">return true</a>https://stackoverflow.com/questions/55020484
复制相似问题