window.onload=function(){
var box=document.getElementById('box');
box.onclick = function(){
alert('Lee');
};
};
window.onload=function(){
alert('Lee');
};
window.onload=function(){
alert('Mr.lee');
}
alert(window.onload);//一开始没有注册window.onload,那么就是null
window.onload=function(){
alert('Lee');
};
alert(window.onload);//如果已经有window.onload,打印的是函数function
window.onload=function(){
alert('Mr.lee');
}
alert(typeof window.onload);//一开始没有window.onolad,旧版火狐显示undefined,新版显示object,
window.onload=function(){
alert('Lee');
};
alert(typeof window.onload);//如果已经有window.onload,所有浏览器都会显示function
window.onload=function(){
alert('Mr.lee');
}
window.onload=function(){
alert('Lee');
};
if(typeof window.onload=='function'){
var saved=null;//保存上一个事件对象
saved=window.onload;
}
//saved 就是window.onload,saved()相当于window.onload(),但是window.onload()不能执行的
//所以saved()相当于window.onload=function(){}
window.onload=function(){
if(saved){
saved();//执行上一个事件 window.onload=function(){}
}
alert('Mr.lee'); //执行本事件
}
window.onload=function(){
var box=document.getElementById('box');
box.className="red";
box.onclick=function(){
alert('Lee'); //只执行了一次
blue.call(this);//通过匿名函数执行某一函数,那么里面的this就是代表的window,所以可以通过call传递
};
}
function blue(){
this.className="blue";
this.onclick=red;
}
function red(){
this.className="red";
this.onclick=blue;
}
//添加事件函数
//obj相当于window
//type相当于onload
//fn相当于function(){}
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',function(){ //目的达到,每次都执行了,没有被覆盖
// alert('ss');
//});
addEvent(box,'click',blue);
});
function red(){
this.className="red";
addEvent(box,'click',blue);
}
function blue(){
this.className="blue";
addEvent(box,'click',red);
}
//当不停的切换的时候,浏览器突然卡死,并且报错:too much recursion,太多的递归
//因为积累了太多的保存的事件
//解决方案,就是用完的事件,就立刻移除掉
//添加事件函数
//obj相当于window
//type相当于onload
//fn相当于function(){}
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);
}
}
//当不停的切换的时候,浏览器突然卡死,并且报错:too much recursion,太多的递归
//因为积累了太多的保存的事件
//解决方案,就是用完的事件,就立刻移除掉
//移除事件函数
function removeEvent(obj,type){
if(obj['on'+type]){
obj['on'+type]=null;
}
}
addEvent(window,'load',function(){
var box=document.getElementById("box");
//addEvent(box,'click',function(){ //目的达到,每次都执行了,没有被覆盖
// alert('ss');
//});
addEvent(box,'click',blue);
});
function red(){
this.className="red";
removeEvent(this,'click');
addEvent(box,'click',blue);
}
function blue(){
this.className="blue";
removeEvent(this,'click');
addEvent(box,'click',red);
}
window.addEventListener('load',function(){
alert('Lee');
},false);
window.addEventListener('load',function(){
alert('Mr.Lee');
},false);
window.addEventListener('load',function(){
alert('Mrs.Lee');
},false);
window.addEventListener('load',init,false);
window.addEventListener('load',init,false);
window.addEventListener('load',init,false);
function init(){
alert('Lee');
}
window.addEventListener('load',function(){
var box=document.getElementById('box');
box.addEventListener('click',function(){
alert(this);
},false);
},false);
window.addEventListener('load',function(){
var box=document.getElementById('box');
box.addEventListener('click',blue,false);
},false);
function red(){
this.className="red";
this.removeEventListener('click',red,false);
this.addEventListener('click',blue,false);
}
function blue(){
this.className="blue";
this.removeEventListener('click',blue,false);
this.addEventListener('click',red,false);
}
window.addEventListener('load',function(){
var box=document.getElementById('box');
box.addEventListener('click',function(){
alert('Lee');
},false);
box.addEventListener('click',blue,false);
},false);
window.attachEvent('onload',function(){
alert('Lee');
});
window.attachEvent('onload',function(){
alert('Mr.Lee');
});
window.attachEvent('onload',function(){
alert('Mrs.Lee');
});
window.attachEvent('onload',init);
window.attachEvent('onload',init);
function init(){
alert('Lee');
}
window.attachEvent('onload',function(){
var box=document.getElementById('box');
box.attachEvent('onclick',function(){
//alert(this===box);
alert(this===window); //true
});
});
window.attachEvent('onload',function(){
var box=document.getElementById('box');
box.attachEvent('onclick',blue);
});
function red(){
var that=window.event.srcElement;
that.className="red";
that.detachEvent('onclick',red);
that.attachEvent('onclick',blue);
}
function blue(){
var that=window.event.srcElement;
that.className="blue";
that.detachEvent('onclick',blue);
that.attachEvent('onclick',red);
}
window.attachEvent('onload',function(){
var box=document.getElementById('box');
box.onclick=function(evt){ //传统方法IE无法通过参数获取evt
alert(evt);//undefined
}
box.attachEvent('onclick',function(evt){
alert(evt);//object
alert(evt.type);//click
alert(evt.srcElement.tagName);//DIV
alert(window.event.srcElement.tagName);//DIV
});
});
function addEvent(obj,type,fn){
if(obj.addEventListener){
obj.addEventListener(type,fn,false);
}else if(obj.attachEvent){
obj.attachEvent('on'+type,fn);
}
}
function removeEvent(obj,type,fn){
if(obj.removeEventListener){
obj.removeEventListener(type,fn,false);
}else if(obj.detachEvent){
obj.detachEvent('on'+type,fn);
}
}
function getTarget(evt){
if(evt.target){
return evt.target;
}else if(window.event.srcElement){
return window.event.srcElement;
}
}
addEvent(window,'load',function(){
var box=document.getElementById('box');
addEvent(box,'click',blue);
});
function red(evt){
var that=getTarget(evt);
that.className="red";
removeEvent(that,'click',red);
addEvent(that,'click',blue);
}
function blue(evt){
var that=getTarget(evt);
that.className="blue";
removeEvent(that,'click',blue);
addEvent(that,'click',red);
}
addEvent(window,'load',function(){
var box=document.getElementById('box');
addEvent(box,'mouseover',function(evt){
alert(evt.relatedTarget); //得到移入box最近的那个DOM对象
});
addEvent(box,'mouseout',function(evt){
alert(evt.relatedTarget); //从box移出最近的那个DOM对象
});
});
addEvent(window,'load',function(){
var box=document.getElementById('box');
addEvent(box,'mouseover',function(){
alert(window.event.fromElement.tagName); //得到移入box最近的那个DOM对象
});
addEvent(box,'mouseout',function(){
alert(window.event.toElement.tagName); //从box移出最近的那个DOM对象
});
});
function getTarget(evt){
var e=evt || window.event;
if(e.srcElment){ //IE
if(e.type=='mouseover'){
return e.fromElement.tagName;
}else if(e.type="mouseout"){
return e.toElement.tagName;
}
}else if(e.relatedTarget){ //w3c
return e.relatedTarget;
}
}
link.onclick=function(){
alert('Lee');
return false;
}
link.onclick=function(evt){
evt.preventDefault;//w3c,阻止默认行为
alert('Lee');
}
link.onclick=function(evt){
window.event.returnValue=false;//IE,阻止默认行为
alert('Lee');
}
function preDef(evt){
var e=evt || window.event;
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue=false;
}
}
function preDef(evt){
var e=evt || window.event;
if(e.preventDefault){
e.preventDefault();
}else{
e.returnValue=false;
}
}
addEvent(window,"load",function(){
var body=document.getElementsByTagName('body')[0];
addEvent(body,'contextmenu',function(evt){
preDef(evt);
})
});
addEvent(window,'beforeonload',function(){
preDef(evt);
});
addEvent(document,'mousewheel',function(evt){ //非火狐
alert(getWD(evt));
});
addEvent(document,'DOMMouseScroll',function(evt){ //火狐
alert(getWD(evt));
});
function getWD(evt){
var e=evt|| window.event;
if(e.wheelDelta){
return e.wheelDelta;
}else if(e.detail){ //火狐
return -evt.detail*30;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有