<a href="#/html" rel="external nofollow" rel="external nofollow" >html课程</a> <a href="#/css" rel="external nofollow" rel="external nofollow" >css课程</a>
window.onload = function(){
//当hash发生变化的时候, 会产生一个事件 onhashchange
window.onhashchange = function(){
console.log( '你的hash改变了' );
//location对象是 javascript内置的(自带的)
console.log( location );
}
}
<input type="button" value="33选5"> <div></div>
window.onload = function(){
var oBtn = document.querySelector("input");
var oDiv = document.querySelector("div");
//33->max 5->num
function buildNum( max, num ){
var arr = [];
for( var i = 0; i < max; i++ ){
arr.push( i + 1 );
}
var target = []; //从1-33这33个数字中 随机选出5个数
for( var i = 0; i < num; i++ ){
target.push( arr.splice( Math.floor( Math.random() * arr.length ), 1 ) );
}
return target;
}
oBtn.onclick = function(){
var num = buildNum( 33, 5 );
oDiv.innerHTML = num;
location.hash = num;
}
window.onhashchange = function(){
oDiv.innerHTML = location.hash.substring(1);
}
}
header,
footer {
height: 100px;
background: #ccc;
}
section {
width: 60%;
height: 400px;
background: #eee;
float: left;
}
sidebar {
width: 40%;
height: 400px;
background: #999;
float: left;
}
.clear {
clear: both;
}
<header>
头部
</header>
<section>
<ul>
<li><a href="#/" rel="external nofollow" >全部</a></li>
<li><a href="#/html" rel="external nofollow" rel="external nofollow" >html课程</a></li>
<li><a href="#/css" rel="external nofollow" rel="external nofollow" >css课程</a></li>
<li><a href="#/javascript" rel="external nofollow" >javascript课程</a></li>
</ul>
</section>
<sidebar>
右边
</sidebar>
<div class="clear"></div>
<footer>
底部
</footer>
(function(){
var Router = function(){
/*
this.routes['/'] = function(){}
this.routes['/html'] = function(){}
*/
this.routes = {};//用来保存路由
this.curUrl = ''; //获取当前的hash
}
Router.prototype.init = function(){ //监听路由变化
//call,apply
window.addEventListener( 'hashchange', this.reloadPage.bind(this) );
}
Router.prototype.reloadPage = function(){
this.curUrl = location.hash.substring(1) || '/';
this.routes[this.curUrl]();
}
Router.prototype.map = function( key, callback ){ //保存路由对应的函数
this.routes[key] = callback;
// console.log( this.routes );
}
window.Router = Router;
})();
var oRouter = new Router();
oRouter.init();
oRouter.map( '/', function(){
var oSidebar = document.querySelector("sidebar");
oSidebar.innerHTML = 'ghostwu提供html,css,javascript从0基础到精通系列课程,只要会开关机,就能学会';
});
oRouter.map('/html', function(){
var oSidebar = document.querySelector("sidebar");
oSidebar.innerHTML = 'ghostwu提供html5从入门到精通的课程';
});
oRouter.map('/css', function(){
var oSidebar = document.querySelector("sidebar");
oSidebar.innerHTML = 'ghostwu提供从入门到玩转css3课程';
});
oRouter.map('/javascript', function(){
var oSidebar = document.querySelector("sidebar");
oSidebar.innerHTML = "ghostwu提供从0基础到精通javascript系列课程";
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有