转自:http://blog.sk80.com/post/281.html
1、获取事件的目标是不一样的?
????位于事件中心对象成为目标(target),假设 元素分配onclick事件处理函数,触发click事件时, 就被认为 是目标
2、获取字符代码
????IE和DOM都支持event对象的keyCode属性,它会返回按下按键的数值代码。如果按键代表一个字符(非shift,alt,ctrl等) IE的keyCode将返回字符的代码(等于他的unicode码)
3、阻止某个事件的默认行为
4、停止事件的复制(冒泡)
????位于事件中心对象成为目标(target),假设 元素分配onclick事件处理函数,触发click事件时, 就被认为 是目标
????IE
????????????????var?oTarget=oEvent.srcElement;?//Masintosh的IE同时支持srcElement和target属性
????DOM
????????????????DOM兼容的游览器上目标包含在target属性中
????????????????var?oTarget=oEvent.target;
????????????????var?oTarget=oEvent.srcElement;?//Masintosh的IE同时支持srcElement和target属性
????DOM
????????????????DOM兼容的游览器上目标包含在target属性中
????????????????var?oTarget=oEvent.target;
2、获取字符代码
????IE和DOM都支持event对象的keyCode属性,它会返回按下按键的数值代码。如果按键代表一个字符(非shift,alt,ctrl等) IE的keyCode将返回字符的代码(等于他的unicode码)
????????????IE
????????????????????var?iCharCode?=?event.keyCode
????????????DOM
????????????????????在DOM兼容的游览器中,按键的代码和字符会有一个分离,要获取字符代码,使用charCode属性
????????????????????var?iCharCode?=event.charCode
????????????????????var?iCharCode?=?event.keyCode
????????????DOM
????????????????????在DOM兼容的游览器中,按键的代码和字符会有一个分离,要获取字符代码,使用charCode属性
????????????????????var?iCharCode?=event.charCode
3、阻止某个事件的默认行为
????????IE
????????????????要在IE中阻止某个事件的默认行为,必须将returnValue属性设置为false
????????????????????oEvent.returnValue=false;
???????DOM
????????????????????要调用preventDefault()方法
????????????????????oEvent.preventDefault();
?????????????????? 比如在右击页面的时候可以阻止事件的默认行为,只要阻止contextmenu事件的默认行为就可以了。
????????????????要在IE中阻止某个事件的默认行为,必须将returnValue属性设置为false
????????????????????oEvent.returnValue=false;
???????DOM
????????????????????要调用preventDefault()方法
????????????????????oEvent.preventDefault();
?????????????????? 比如在右击页面的时候可以阻止事件的默认行为,只要阻止contextmenu事件的默认行为就可以了。
4、停止事件的复制(冒泡)
???????IE
????????????????????在IE中,要阻止事件的进一步冒泡,必须设置cancelBubble属性为true
????????????????????oEvent.cancelBubble=true;????????
???????DOM
????????????????????在mozilla中,只需要调用stopPropagation()方法即可
????????????????????oEvent.stopPropagation()
????????????????
????????????????<html?onclick="alert('html')">
????????????????????<body?onclick="alert('body')">
????????????????????????<input?type=button?value="click"?onclick="alert('button')">
????????????????????</body>
????????????????</html>
????????????????点击button会依次出现?button,body,html的提示
????????????????如果增加如下函数,则可阻止事件的复制(冒泡)
????????????????
????????????????function?handleClick(oEvent)
????????????????{
????????????????????alert('button');
????????????????????if(window.event)
????????????????????{
????????????????????????oEvent=window.event;
????????????????????????oEvent.cancelBubble=true;
????????????????????}
????????????????????else
????????????????????{
????????????????????????oEvent.stopPropagation();
????????????????????}
????????????????}
????????????????
????????????????<input?type=button?value="click"?onclick="handleClick(event)">
????????????????则只会出现?button的提示
????????????????????在IE中,要阻止事件的进一步冒泡,必须设置cancelBubble属性为true
????????????????????oEvent.cancelBubble=true;????????
???????DOM
????????????????????在mozilla中,只需要调用stopPropagation()方法即可
????????????????????oEvent.stopPropagation()
????????????????
????????????????<html?onclick="alert('html')">
????????????????????<body?onclick="alert('body')">
????????????????????????<input?type=button?value="click"?onclick="alert('button')">
????????????????????</body>
????????????????</html>
????????????????点击button会依次出现?button,body,html的提示
????????????????如果增加如下函数,则可阻止事件的复制(冒泡)
????????????????
????????????????function?handleClick(oEvent)
????????????????{
????????????????????alert('button');
????????????????????if(window.event)
????????????????????{
????????????????????????oEvent=window.event;
????????????????????????oEvent.cancelBubble=true;
????????????????????}
????????????????????else
????????????????????{
????????????????????????oEvent.stopPropagation();
????????????????????}
????????????????}
????????????????
????????????????<input?type=button?value="click"?onclick="handleClick(event)">
????????????????则只会出现?button的提示