通过js实现页面的跳转页面跳转
使用 window.location.href
设置 window.location.href 为新的 URL 可以实现页面跳转。这是最常见的方式,支持相对路径和绝对路径。
window.location.href = "https://example.com";
使用 window.location.replace
window.location.replace 会替换当前页面,跳转到新页面且不会在浏览器历史记录中留下当前页面的记录。
window.location.replace("https://example.com");
使用 window.location.assign
window.location.assign 加载新页面,并在浏览器历史记录中保留当前页面,用户可以通过后退按钮返回。
window.location.assign("https://example.com");
使用 window.open
window.open 在新窗口或标签页中打开页面,可以通过参数控制打开方式。
window.open("https://example.com", "_blank");
使用 meta 标签自动跳转
在 HTML 的 <head> 中添加 <meta> 标签,设置 http-equiv="refresh" 实现自动跳转。
<meta http-equiv="refresh" content="3;url=https://example.com">
使用表单提交跳转
通过 JavaScript 动态提交表单实现跳转,适用于需要传递数据的场景。
document.getElementById("myForm").submit();
使用 history.pushState 或 history.replaceState
history.pushState 和 history.replaceState 可以修改浏览器历史记录,结合 popstate 事件实现无刷新跳转。
history.pushState({}, "", "newpage.html");
使用锚点跳转
通过修改 window.location.hash 实现页面内的锚点跳转。
window.location.hash = "#section1";






