用JS实现跳转网页

写了三个页面,demoA.html  可以跳转到pageA.html和pageB.html,pageA和pageB都可以回到demoA.html

demoA.html 代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <style>
 
	.curPageStyle{
		margin-top: 300px;
	}
  </style>
  <body>
    
	<button onclick="jumpA()">Go to pageA</button>

	
	<div class="curPageStyle">
		<button  onclick="currentPage()">current page: </button>
		<div id="curPageHref" style="display: inline-block"></div>
	</div>
    
    
    <script>
     
	 function jumpA(){
		window.location.href = "./pageA.html";
	 }
	 
	
	 
	 function currentPage(){
		document.getElementById("curPageHref").textContent = window.location.href;
	 }
	 
    </script>
  </body>
</html>

效果如下:

window.location.href可以显示出当前页面URL。

 

pageA.html代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <style>
  
  
	.pageA{
		width: 200px;
		height: 200px;
		margin: 0 auto;
		background-color: green
	}
  </style>
  <body>
    <button onclick="jump()">Go back to demoA</button>
	
	<div class="pageA"></div>
	
	<button onclick="jumpB()">Go to pageB</button>
    
    <script>
     function jump(){
		//window.location.href = "./demoA.html";
		//window.history.go(-1);
		window.history.back();
	 }
	 
	 function jumpB(){
		window.location.href = "./pageB.html";
	 }
	 
    </script>
  </body>
</html>

通过

         //window.location.href = "./demoA.html";
        //window.history.go(-1);
        window.history.back();

这三种方式都可以回到demoA.html页面。

 

pageB.html 代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <style>
  
	.pageB{
		width: 200px;
		height: 200px;
		margin: 0 auto;
		background-color: yellow
	}
  </style>
  <body>
    <button onclick="jump()">Go back to demoA</button>
	<div class="pageB"></div>
    
    <script>
     function jump(){
		//window.location.href = "./demoA.html";
		window.history.go(-2);
	 }
	 
    </script>
  </body>
</html>

从pageB.html可以直接回到demoA.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值