location.href页面不跳转解决方案,附加加两个小案例

关于window.location.href 不跳转做了一些小总结

首先说跳转的条件,在<a>标签中

首先要要求  a 标签如下:

<a οnclick="函数名()" href="javascript:void(0)"></a>

而 a 标签的 href 属性一定不能写错,

而经过我的实验  href 的属性值 取 “#” 或者设置成 “javascript:void(0)” 第二个一定不能写错,否则跳转不成功。

href="javascript:void(0)"
href="#"

而函数就很简单了

<script type="text/javascript">
	function 函数名() {
		location.href="XXXXXXX";
		//或者是window.location.href="XXXXXXX";
	}
</script>

对比一下,没有写错的地方一定能跳转成功

写了两个互相跳转的测试页面:

demo1

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>location.href跳转实例</title>
</head>
<script type="text/javascript">
	function baidu2() {
		window.location.href = "demo2.jsp";
	}
	
	function baidu() {
		//window 可以省略  所以这也写也是可以的
		location.href = "demo2.jsp";
	}
</script>
<body>
	<a οnclick="baidu()" href="#" >跳转测试1(# + location.href 组合)</a><br>
	<a οnclick="baidu2()" href="javascript:void(0)" >跳转测试2(javascript:void(0) + window.location.href 组合)</a>
	<hr>
	<h1>我是demo1</h1>
</body>
</html>

demo2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>location.href跳转实例</title>
</head>
<script type="text/javascript">
	function baidu() {
		window.location.href = "demo1.jsp";
	}
	
	function baidu2() {
		//window 可以省略  所以这也写也是可以的
		location.href = "demo1.jsp";
	}
</script>
<body>
	<a οnclick="baidu()" href="#" >跳转测试1(# + window.location.href 组合)</a><br>
	<a οnclick="baidu2()" href="javascript:void(0)" >跳转测试2(javascript:void(0) + location.href 组合)</a>
	<hr>
	<h1>我是demo2</h1>
</body>
</html>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值