<table id="tb"> <tr> <td>1111111</td> <td>1111111</td> <td>1111111</td> <td>1111111</td> <td>1111111</td> <td>1111111</td> </tr> </table> </body> <script type="text/javascript"> var td = document.getElementById("tb").getElementsByTagName("tr")[0].getElementsByTagName("td") for(var i = 0 ; i < td.length ; i ++) { td[i].onclick = function(){alert("x")} } for(var i = 0 ; i < td.length ; i ++) { if(i == 0 || i ==td.length - 1) td[i].onclick = null } </script>
IE event.stopPropagation()阻止冒泡事件
在火狐Firefox、opera、IE下阻止冒泡事件是不同的代码的,火狐下使用的是event.stopPropagation(),而IE下使用的是cancelBubble,jQuery
可以使用e.stopPropagation()就可以兼容了,如果是纯粹的JavaScript需要下面的代码来统一:
if (event.stopPropagation) { // this code is for Mozilla and Opera event.stopPropagation(); } else if (window.event) { // this code is for IE window.event.cancelBubble = true; }
时间: 2024-10-07 20:47:50