javascript 事件冒泡的补充
2011-05-27 22:19
155 查看
scroll在IE8-9中无法捕获,由于又不能冒泡,因此无法使用事件代理.它在FF下可以通过捕获来处理.
focus与blur事件在IE下可以通过focusin与focusout事件来模拟,但对于那些不能非表单元素或链接或图片等一般元素,我们需要给它设置tabIndex,才能让它拥有捕获焦点或失去焦点的能力.在某些非常新的标准浏览器中,可以使用DOMFocusIn,DOMFocusOut来模拟,但这种不能使用onXXX调用的事件随时可能废弃,因此我也不会用它们来模拟.focus,blur的事件代理.
<!doctype html>
<html>
<head>
<title>事件系统笔记 司徒正美</title>
<style>
div.test {
width:400px;
height:320px;
margin:0 15px;
background-color:#D6EDFC;
overflow: auto;
float:left;
}
div.test1{
height: 200px;
background: red;
}
</style>
<script>
window.onload = function(){
var el = document.getElementById("test");
el.addEventListener("DOMFocusIn",function(e){
console.log(e.type)
},false)
el.addEventListener("DOMFocusOut",function(e){
console.log(e.type)
},false);
el.addEventListener("focus",function(e){
console.log(e.type)
},false)
el.addEventListener("blur",function(e){
console.log(e.type)
},false);
}
</script>
</head>
<body>
<h1>mouseenter与mouseleave</h1>
<div class="test" id="test" tabIndex="-1" >
<div class="test1" tabIndex="-1" ></div>
</div>
</body>
</html>
运行代码
scroll 只能用于两个DIV之间 或window中,这时要求存在滚动条.在IE中,不能冒泡也无法用捕获,在标准浏览器中冒泡情况也非常混乱,只重要的是要判定是否存在滚动条,因此也不打算支持其事件代理.
focus与blur事件在IE下可以通过focusin与focusout事件来模拟,但对于那些不能非表单元素或链接或图片等一般元素,我们需要给它设置tabIndex,才能让它拥有捕获焦点或失去焦点的能力.在某些非常新的标准浏览器中,可以使用DOMFocusIn,DOMFocusOut来模拟,但这种不能使用onXXX调用的事件随时可能废弃,因此我也不会用它们来模拟.focus,blur的事件代理.
<!doctype html>
<html>
<head>
<title>事件系统笔记 司徒正美</title>
<style>
div.test {
width:400px;
height:320px;
margin:0 15px;
background-color:#D6EDFC;
overflow: auto;
float:left;
}
div.test1{
height: 200px;
background: red;
}
</style>
<script>
window.onload = function(){
var el = document.getElementById("test");
el.addEventListener("DOMFocusIn",function(e){
console.log(e.type)
},false)
el.addEventListener("DOMFocusOut",function(e){
console.log(e.type)
},false);
el.addEventListener("focus",function(e){
console.log(e.type)
},false)
el.addEventListener("blur",function(e){
console.log(e.type)
},false);
}
</script>
</head>
<body>
<h1>mouseenter与mouseleave</h1>
<div class="test" id="test" tabIndex="-1" >
<div class="test1" tabIndex="-1" ></div>
</div>
</body>
</html>
运行代码
scroll 只能用于两个DIV之间 或window中,这时要求存在滚动条.在IE中,不能冒泡也无法用捕获,在标准浏览器中冒泡情况也非常混乱,只重要的是要判定是否存在滚动条,因此也不打算支持其事件代理.
//让IE与w3c支持focus与blur与select的代理 "focus_focusin,blur_focusout,select_selectstart".replace(/\w+/g,function(types){ types = types.split("_"); events.special[ types[0] ] = { liveType: DOC.dispatchEvent ? types[0] : types[1], livePhase:!!DOC.dispatchEvent, liveSetup: function(node, type, _, phase){ enableFocusin(node); dom.bind(node,type,function(e){ e = events.fix(e); dom.log("让IE与w3c支持focus与blur与select的代理") e.type = types[0]; events.handle.call(this,e); },phase); } } });
相关文章推荐
- JavaScript事件冒泡简介及应用
- [转]简析JavaScript事件、以及捕获和冒泡
- 浅谈javascript事件取消和阻止冒泡
- javascript事件冒泡实例详解和jquery阻止事件冒泡的两种方法
- javascript阻止事件冒泡和浏览器的默认行为
- 夺命雷公狗---javascript NO:15 事件冒泡3
- JavaScript 阻止事件冒泡传递
- 我的JavaScript回顾之路_05—0210—js和jq绑定事件的区别/事件冒泡
- JavaScript中事件冒泡问题
- JavaScript阻止冒泡事件的事例
- JavaScript事件基础(事件对象,键盘,鼠标,冒泡)
- javascript阻止冒泡事件
- Javascript Event(事件)的传播与冒泡
- JavaScript事件冒泡简介及应用
- 002_javaScript中事件的冒泡(Bubble)
- 【javascript前端】事件冒泡的实际试验及解决方式
- 【转载】JavaScript事件冒泡简介及应用
- javascript事件冒泡
- JavaScript之jQuery-4 jQuery事件(页面加载后执行、事件处理、事件冒泡、事件对象、模拟操作)
- javascript解决多事件冲突的问题(冒泡行为、默认行为)