2013-10-25 33 views
0

我的代碼工作:event.preventDefault() - 未加載<a>

$('.menu a').on('click', function(event){ 
    event.preventDefault(); 
    if($(this).parent().children('ul').is('.deep1')){ 
     var result = $(this).parent().children('ul').html(); 
     $('.dpp1').html('<ul class="menu" style="display: none">'+result+'</ul>'); 
     $('.dpp1').children('.menu').stop().fadeIn() 
    }else if($(this).parent().children('ul').is('.deep2')){ 
     var result = $(this).parent().children('ul').html(); 
     $('.dpp2').html('<ul class="menu" style="display: none">'+result+'</ul>'); 
     $('.dpp2').children('.menu').stop().fadeIn() 
    }else if($(this).parent().children('ul').is('.deep3')){ 
     var result = $(this).parent().children('ul').html(); 
     $('.dpp3').html('<ul class="menu" style="display: none">'+result+'</ul>'); 
     $('.dpp3').children('.menu').stop().fadeIn() 
    } 
}); 

我的問題是如何保持它在創建的元素工作的preventDefault()?

回答

1

試試這個,

$(document).on('click', '.menu a', function (event) { 
    event.preventDefault(); 
    // your code 
}); 
+0

完美的工作,THX! –

3

自錨動態添加,你需要使用event delegation註冊事件處理程序

$('.menu').on('click', 'a', function (event) { 
    event.preventDefault(); 
    //........ 
});