我有这样的结构
<table><tr><td></td><td><input type="button" name="button"></td></tr></table>
<script>
$('tr').click(function(){window.alert('tr');})
$(':input').click(function(){window.alert('input');})
</script>
当我单击按钮时,也会调用 tr click 事件。有没有办法禁用父元素上的点击事件?我找到的唯一解决方案是将类添加到父元素并在其单击函数内检查它。
例如:
<script>
$('tr').click(function(){ if(!$(this).hasClass('disable')) { window.alert('tr'); } $(this).removeClass('disable'); })
$(':input').click(function(){window.alert('input'); $(this).closest('tr').addClass('disable');})
</script>
use .stopPropagation() http://api.jquery.com/event.stoppropagation/
$('tr').click(function(){
window.alert('tr');
});
$(':input').click(function(e){
e.stopPropagation();
window.alert('input');
});
demo http://jsfiddle.net/guK5P/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)