这里有很多相同的问题。我试过所有的解决方案-没有任何帮助。
我想打开新的网页在一些标签与JavaScript.这里是我的HTML:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" ></script>
<script src="functions.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
<label>
<input type="image" name="imageField" id="loginClient" onclick="testUrl()" src="images/index_33.gif" />
</label>
...
</body>
</html>
字符串
以下是我的functions.js:
function testUrl()
{
window.location = "content.html";
}
function loginClient()
...//more functions
型
我也试过其他方法:
window.location.replace("content.html")
top.location="content.html"
window.location.assign("content.html")
window.open("content.html", "_self")
window.location.href="content.html"
window.event.returnValue = false;
window.location = "content.html";
setTimeout(function(){window.location.assign("content.html");return false;},500)
型
什么都不起作用,我在Opera,Firefox(都在Windows中)和Chrome(Debian)中试过。但是如果我设置:
window.open("content.html")
型
但在新的选项卡,不是解决方案,我需要它在同一个选项卡。
但是如果我尝试用Firebug调试它,并总是点击“Step into”,那么所有的解决方案都不起作用。如果我在重定向之前设置alert(),它也会起作用。
控制台显示没有错误。我不知道,帮帮忙。
下一篇:感谢@lonesomeday!
这就是解决方案:
$(document).ready(function(){
$("#loginClient").click(function(event){
event.preventDefault();
window.location = "content.html";
});
});
型
1条答案
按热度按时间xfyts7mz1#
您在
<input type="image">
元素上有这个onclick
属性。图像输入实际上是submit
按钮。当您单击它时,它被激活,表单被提交。当表单被提交时,它会取消任何正在进行的HTTP请求,例如您的window.location
调用。因此,您需要阻止表单提交。
快速而肮脏的方法是从事件处理程序返回
false
。类似于这样:字符串
关于JS:
型
更好的方法是将事件处理程序与JavaScript绑定并使用
event.preventDefault
。