문제

My code:

 $('.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() 
    }
 });

My question is how to keep it working preventDefault() in created elements?

도움이 되었습니까?

해결책 2

Try this,

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

다른 팁

Since the anchors are added dynamically, you need to use event delegation to register the event handler

$('.menu').on('click', 'a', function (event) {
    event.preventDefault();
    //........
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top