方法1:使用location.href 语法: location.href=URL 示例 !DOCTYPE htmlhtml headmeta charset=UTF-8 /head body p这是ilocation.href/i方式的示例/p button onclick=myFunc()点击这里/button !--重定向到其他网页的脚本--
方法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>

以上就是js怎么跳转到新页面?的详细内容,更多请关注米米素材网其它相关文章!

发表评论