本文摘自PHP中文网,作者青灯夜游,侵删。
js跳转到新页面的方法:1、使用location.href="新页面URL"来跳转;2、使用location.replace("新页面URL")来跳转;3、使用location.assign("新页面URL")来跳转。
方法1:使用location.href
语法:
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!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()方法跳转到其他网页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!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()方法跳转到其他网页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!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怎么跳转到新页面?的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
js proxy 的优势以及使用场景
一分钟了解js中int和string的转换(附代码)
js实现统计字符出现的次数并去重的方法
js如何实现原生map
浅谈js中的bind()
js怎么补齐数字
网页代码中js和css指的是什么
javascript怎么读取map的值
js干货分享―-this指向问题
javascript时间戳如何转化为日期
更多相关阅读请进入《js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » js怎么跳转到新页面?