码迷,mamicode.com
首页 > 其他好文 > 详细

实现事件来回切换

时间:2015-10-19 12:26:00      阅读:110      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!