js跳转到新页面的方法:1、使用location.href="新页面URL"来跳转;2、使用location.replace("新页面URL")来跳转;3、使用location.assign("新页面URL")来跳转。

方法1:使用location.href
语法:
location.href="URL"
示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>这是<i>location.href</i>方式的示例</p>
<button onclick="myFunc()">点击这里</button>
<!--重定向到其他网页的脚本-->
<script>
function myFunc() {
window.location.href="https://www.php.cn";
}
</script>
</body>
</html>
方法2:使用location.replace()
语法:
location.replace("URL")示例:使用location.replace()方法跳转到其他网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>这是<i>location.replace()</i>方式的示例</p>
<button onclick="myFunc()">点击这里</button>
<!--重定向到其他网页的脚本-->
<script>
function myFunc() {
location.replace("https://www.php.cn");
}
</script>
</body>
</html>
方法3:使用location.assign()
语法:
location.assign("URL")示例:使用location.assign()方法跳转到其他网页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>这是<i>location.assign()</i>方式的示例</p>
<button onclick="myFunc()">点击这里</button>
<!--重定向到其他网页的脚本-->
<script>
function myFunc() {
location.assign("https://www.php.cn");
}
</script>
</body>
</html>
推荐教程:《JavaScript视频教程》
以上就是js怎么跳转到新页面?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号