关于取消冒泡的问题~请教大家了
JScript code
<script type="text/javascript">
var oDiv=document.getElementById('div1');
function addEvent(obj, ev, fn){
if(obj.attachEvent){
obj.attachEvent('on'+ev, fn);
}else{
obj.addEventListener(ev, fn, false);
}
}
function cancelBub(ev){
var ev=ev||window.event;
if(ev.cancelBubble){
ev.cancelBubble=true;
}else{
ev.stopPropagation();
}
}
function aa(){
alert('aa');
}
function bb(){
alert('bb');
}
function cc(){
alert('cc');
};
addEvent(document, 'click',aa)
addEvent(document, 'click',bb)
addEvent(oDiv, 'click',cc)
</script>
cancelBub是取消冒泡的函数,那我如何只取消addEvent(oDiv, 'click',cc)这个的冒泡呢?请教了~~
------解决方案--------------------<script type="text/javascript">
var oDiv=document.getElementById('div1');
function addEvent(obj, ev, fn){
if(obj.attachEvent){
obj.attachEvent('on'+ev, fn);
}else{
obj.addEventListener(ev, fn, false);
}
}
function cancelBub(ev){
var ev=ev||window.event;
if(!ev.cancelBubble){
ev.cancelBubble=true;
}else{
ev.stopPropagation();
}
}
function aa(){
alert('aa');
}
function bb(){
alert('bb');
}
function cc(){
cancelBub();
alert('cc');
}
addEvent(document, 'click',aa);
addEvent(document, 'click',bb);
addEvent(oDiv, 'click',cc);
</script>
------解决方案--------------------我改了两处,一处cancelBub函数
if(!ev.cancelBubble)
另一处function cc()里加上cancelBub();
------解决方案--------------------如果ev.cancelBubble为假才需要把它设为true啊
------解决方案--------------------因为cancelBubble默认是false