检查一下代码
来源:3-3 编程练习
weixin_慕的地5241954
2019-09-02 22:10:46
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index1.html</title> </head> <body> <!--因涉及多个页面,请在本机上写代码,并在自己的浏览器上运行该效果--> <a href="index2.html">前往页面2.html</a> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index2.html</title> </head> <body> <!--因涉及多个页面,请在本机上写代码,并在自己的浏览器上运行该效果--> <a href="index3.html">前往页面3.html</a><br/><br/> <input type="button" value="回到页面1"> <script> var btn=document.getElementsByTagName("input")[0]; btn.onclick=function(){ //location.href="index1.html"; history.back(); } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index3.html</title> </head> <body> <!--因涉及多个页面,请在本机上写代码,并在自己的浏览器上运行该效果--> <input type="button" value="回到页面1"> <input type="button" value="回到页面2"> <script> var btn=document.getElementsByTagName("input")[0], btn2=document.getElementsByTagName("input")[1]; btn.onclick=function(){ //location.href="index1.html"; history.go(-2); } btn2.onclick=function(){ //location.href="index2.html"; history.go(-1); } </script> </body> </html>
2回答
同学你好!
代码效果没有问题。history的作用是保存浏览器历史信息;location对象的作用是提供浏览器当前浏览资源的地址信息。
其实在跳转页面上效果是一样的,区别就是history.go()括号中一般写数字,location.href()括号中一般写url地址。常用的是history.go这个方法。
如果帮助到了你,欢迎采纳,祝学习愉快~
weixin_慕的地5241954
提问者
2019-09-02
用location.href=“”这种方法也行,那他们的区别是什么?分别在什么情况下使用?
相似问题