dswe30290 2013-09-24 20:00
浏览 31
已采纳

fadeOut使用jquery动态加载div

Im writing a small app, and im having problems deleting (as in fading out) a dynamically loaded div using jquery.

The problem only presents itself when i add a new content box, if i reload the page and the content box gets rendered not dynamically but normally (as in from the database query), it deletes just fine(as in fade out), but when the div is added anew, i cant get it out.

$result = '<div class="my-music-item">'. $app['twig']->render('soundcloud-player.twig', array(
            'url'=>$mix->getUrl(),
            'player_type'=>'artwork',
            'url'=>$mix->getUrl(),
            'color'=>'ff0948',
            'height'=>'200',
            'width'=>'200'
        )) . '<p class="delete-mix-wrapper"><a class="delete-mix" data-mix-id="'.$mix->getId().'" data-post-to="'.$app['url_generator']->generate('delete-mix-post').'" href="javascript:;">Delete</a></p>' . '</div>';

        return new Response(json_encode(array(
            'html'=>$result,
            'status'=>'true'
        )));

thats the code that creates the div dynamically.

yet when I click delete, nothing happens.

here comes the code that handles the post request through js.

$('a.delete-mix').on('click', function() {
    var parent = $(this).closest('div.my-music-item');
    $.post($(this).attr('data-post-to'), { mix_id: $(this).attr('data-mix-id') })
    .done(function(data) {
        parent.css('background-color', 'pink');
        parent.fadeOut('fast');
    });
});

I have been reading a lot, without much luck! also first my code looked a bit differently, it didnt had the

.on('click', function() { 

but rather was

.click(function() {

Thanks in advance for your input!

  • 写回答

1条回答 默认 最新

  • dongwu8064 2013-09-24 20:18
    关注

    @PSL is correct. A common problem with adding event handlers using jQuery is that the element does not exist on the page when the JS is run, which means that the event handler was not bound to that element, and nothing happens.

    If you change the line

    $('a.delete-mix').on('click', function() { ... } )
    

    to

    $( document ).on( 'click', 'a.delete-mix', function() { ... } )
    

    then you shouldn't have any trouble.

    The second argument being passed into the on() method acts as a filter to the original selector that you bind your event handler to. To explain the example I provided in plain english, you are telling the browser to listen for clicks everywhere on the page, and check if what was clicked happens to be an <a> element with a class of delete-mix. If you did happen to click on a.delete-mix, then the callback function will run. If you didn't, then nothing will happen.

    This is not exactly the best solution. Adding a global click handler to the document can cause problems. A better solution would be to replace document with something more specific, such as a container element that is wrapping the dynamic boxes you are adding. Without more code from you I'm not entirely sure what that selector should be.

    For more information, you should read the jQuery documentation about the on() method.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据