使用jquery从一个html页面重定向到另一个页面

Redirect from one html page to another page using jquery

本文关键字:html 重定向 另一个 一个 jquery 使用      更新时间:2023-09-26

我在本地创建了2个静态html页面,demo1.htmldemo2.html

我需要从一个页面重定向到另一个页面。最初,我打开了我的demo1.html页面(url是file:///d:/app/demo1.html)。

现在点击这个页面上的一个按钮,我想重定向到demo2.html

我试着:

$( "#submit" ).click(function() {
    window.location.href = "/demo2.html";
});

,但它没有工作。我需要做什么改变才能使它起作用?

在新url的开头去掉/。

如果你像你说的那样从文件加载,写入"/demo2.html"将映射到file:///d:/demo2.html

如果你写

 $( "#submit" ).click(function() {
     window.location.href = "demo2.html";
 });

这将映射到你启动应用程序的文件夹,即file:///D:/app/demo2.html

试试这个

 $('#submit').click(function() {
     window.location = '/demo2.html';
 };