1.阻止事件冒泡,使成为捕获型事件触发机制.
取消tr里面子元素响应tr定义的事件,在tr子元素里面禁止气泡事件就可以,子元素事件中加入window.event.cancelBubble=true 即可!
例子:
表格行有一个单击事件,例如:
<table> <tr onclick="alert('hello world')"> <td><input type="button" value="增加" onclick="alert('你好!')"/></td> <td>111</td> <td>222</td> <td>333</td> </tr>
</table>
上面的表格当点击其它单元格时都会弹出“hello world”,怎样让点击“增加”时不让弹出这个警告框?
在td或者input元素的事件中加入“window.event.cancelBubble = true”即可。
例如:<td onclick="window.event.cancelBubble=true;"><input type="button" value="增加" onclick="alert('你好!')"/></td>
或者:<td><input type="button" name="add" onclick="alert('你好!');window.event.cancelBubble=true;"/></td>
多浏览器支持:
function stopBubble(e){
if(document.all)
window.event.cancelBubble=true; //IE
else
e.stopPropagation(); //支持火狐
}
或者
function
stopBubble(e) {
//如果提供了事件对象,则这是一个非IE浏览器
if
( e && e.stopPropagation )
//因此它支持W3C的stopPropagation()方法
e.stopPropagation();
else
//否则,我们需要使用IE的方式来取消事件冒泡
window.event.cancelBubble =
true
;
}
2、阻止浏览器的默认行为
function stopDefault( e ) {
//阻止默认浏览器动作(W3C)
if ( e && e.preventDefault )
e.preventDefault();
//IE中阻止函数器默认动作的方式
else
window.event.returnValue = false;
return false;
}