哈希(不刷新页面,展示不同模板)

简单的哈希路由,不刷新页面,根据哈希值,展示不同模板

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
*{ margin:0; padding:0;}
html , body{ height:100%;}
body{ background-color:#f3f3f3;}
nav{ width:100%; height:60px; background-color:green; text-align:center; position:absolute; left:0; top:0;}
nav a{ display:inline-block; padding:0 10px; height:60px; line-height:60px; color:#fff; vertical-align:middle; text-decoration:none;}

#test{ width:800px; height:400px; line-height:400px; margin:0 auto; text-align:center; font-size:36px; border:#e1e1e1 solid 1px; border-radius:5px; background-color:#fff; overflow-y:auto; position:fixed; left:0; right:0; top:50%; margin-top:-200px;}

.type1{ color:#06F;}
.type2{ color:#CC9;}
.type3{ color:#93F;}
.default{ color:#603;}
</style>
<script type="text/javascript" src="jquery2.1.js"></script>
</head>

<body>

<nav>
	<a href="#">点我展示默认模板</a>
	<a href="#dfdsf">点我展示默认模板</a>
	<a href="#type1">点我展示默认模板一</a>
	<a href="#type2">点我展示默认模板二</a>
	<a href="#type3">点我展示默认模板三</a>
</nav>

<div id="test">对应模板内容加载在这里哦~~~</div>

<script id="type1" type="text/html">
	<div class="type1">我是默认模板一~~~~</div>
</script>
<script id="type2" type="text/html">
	<div class="type2">我是默认模板二~~~~</div>
</script>
<script id="type3" type="text/html">
	<div class="type3">我是默认模板三~~~~</div>
</script>
<script id="default" type="text/html">
	<div class="default">我是默认模板~</div>
</script>
<script type="text/javascript">
$(function(){
	var hashMsg = location.hash;
	hashPage(hashMsg);
	$(window).on('hashchange', function (e) {
		hashMsg = location.hash;
		hashPage(hashMsg);
	})
});
function hashPage(hashMsg){
	var hashArr = ['#type1','#type2','#type3'];
	var demo = $('#test');
	hashMsg = hashArr.indexOf(hashMsg) > -1 ? hashMsg : '#default';
	demo.html($(hashMsg).html());
	//demo.append($(hashMsg).html());
}
</script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值