首页 > 其他 > 详细

实现事件来回切换

时间:2015-10-19 12:25:08      阅读:209      评论:0      收藏:0      [点我收藏+]

1、新建一个层,点击层,希望层的背景色在蓝色和红色之间切换。

<script type="text/javascript">
function addEVent(obj,type,fn){
	var saved=null;
//判断是否之前有事件,如果有,保存下来
	if(typeof obj[‘on‘+type]==‘function‘){
		saved=obj[‘on‘+type];
		
		}	
//执行
	obj[‘on‘+type]=function(){
		if(saved)saved();
		fn.call(this);
		
		
		};
	
}
addEVent(window,‘load‘,function(){
	var box=document.getElementById(‘box‘);
	addEVent(box,‘click‘,toBlue);	
	
	});

function toBlue(){
	this.className=‘blue‘;
	addEVent(this,‘click‘,toRed);
	}
function toRed(){
	this.className=‘red‘;
	addEVent(this,‘click‘,toBlue);
	}
		

</script>

 

<body>
<div id="box" class="red">div</div>

</body>

 

<style type="text/css">
.red{
	width:100px;
	height:100px;
	background:red;
	}

.blue{
	width:100px;
	height:100px;
	background:blue;
	}
</style>

 

实现事件来回切换

原文:http://www.cnblogs.com/jiangwenli/p/4891327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!