因此,当我需要阻止父代在点击子代时的默认动作时,我通常只使用stopPropegation。然而,这似乎对活的元素不起作用。所以我有个想法,当孩子被悬停在上面时,暂时移除父类的onclick属性。我似乎可以很好地删除它,但我似乎无法将它重新添加到元素中......这就是我想出的办法......
$('body').delegate('.button', 'mouseenter mouseout', function(e){
if(e.type=='mouseenter'){
var inside = $(this).parents('.post').attr('onclick');
$(this).parents('.post').attr('onclick','');
}else if(e.type=='mouseout'){
$(this).parents('.post').attr('onclick', inside);
因此,这将成功地删除该元素,但试图将其添加回来总是不成功......它只是出现了空。有谁能帮我一下吗?我完全被卡住了。我也很乐意用其他方法来实现同样的结果。谢谢你。
试试这个 http://jsfiddle.net/kDuNU/4/。
$('div').live('click', function(event) { if ($(event.target).hasClass('button')) return; alert('hello'); $('.a-span').live('click', function (event) { alert($(this).text()); $('div').append('<span class="a-span button">another</span>'); <div> hello <br/> <span class="a-span">world</span> </div>