这里有很多相同的问题。我尝试了所有的解决方案,但都没有帮助。
我想用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进行调试并始终点击“步入”,那么所有的解决方案都可以工作。如果在重定向之前设置alert(),它也可以正常工作。
控制台没有显示任何错误。我不知道怎么办,请帮忙。
已解决:感谢@lonesomeday!
这就是解决方案:
$(document).ready(function(){
$("#loginClient").click(function(event){
event.preventDefault();
window.location = "content.html";
});
});
functions.js
文件(例如,路径是否正确)。 - Jared Farrish