html JavaScript重定向不工作无论如何

sbdsn5lh  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(153)

这里有很多相同的问题。我试过所有的解决方案-没有任何帮助。
我想打开新的网页在一些标签与JavaScript.这里是我的HTML:

  1. <head>
  2. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" ></script>
  3. <script src="functions.js"></script>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Untitled Document</title>
  6. <link href="css.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. ...
  10. <label>
  11. <input type="image" name="imageField" id="loginClient" onclick="testUrl()" src="images/index_33.gif" />
  12. </label>
  13. ...
  14. </body>
  15. </html>

字符串
以下是我的functions.js:

  1. function testUrl()
  2. {
  3. window.location = "content.html";
  4. }
  5. function loginClient()
  6. ...//more functions


我也试过其他方法:

  1. window.location.replace("content.html")
  2. top.location="content.html"
  3. window.location.assign("content.html")
  4. window.open("content.html", "_self")
  5. window.location.href="content.html"
  6. window.event.returnValue = false;
  7. window.location = "content.html";
  8. setTimeout(function(){window.location.assign("content.html");return false;},500)


什么都不起作用,我在Opera,Firefox(都在Windows中)和Chrome(Debian)中试过。但是如果我设置:

  1. window.open("content.html")


但在新的选项卡,不是解决方案,我需要它在同一个选项卡。
但是如果我尝试用Firebug调试它,并总是点击“Step into”,那么所有的解决方案都不起作用。如果我在重定向之前设置alert(),它也会起作用。
控制台显示没有错误。我不知道,帮帮忙。
下一篇:感谢@lonesomeday!
这就是解决方案:

  1. $(document).ready(function(){
  2. $("#loginClient").click(function(event){
  3. event.preventDefault();
  4. window.location = "content.html";
  5. });
  6. });

xfyts7mz

xfyts7mz1#

您在<input type="image">元素上有这个onclick属性。图像输入实际上是submit按钮。当您单击它时,它被激活,表单被提交。当表单被提交时,它会取消任何正在进行的HTTP请求,例如您的window.location调用。
因此,您需要阻止表单提交。
快速而肮脏的方法是从事件处理程序返回false。类似于这样:

  1. onclick="return testUrl()"

字符串
关于JS:

  1. function testUrl()
  2. {
  3. window.location = "content.html";
  4. return false;
  5. }


更好的方法是将事件处理程序与JavaScript绑定并使用event.preventDefault

展开查看全部

相关问题