简体   繁体   English

jQuery-.append之后,.hover在其他元素上不起作用

[英]Jquery - After .append, .hover on other element not working

I wrote a bit of code to handle the insertion of a comment via AJAX. 我写了一些代码来处理通过AJAX插入评论。 Once you have entered the comment, having received the HTML from the server and use .append() to insert it into the DOM, does not seem to be handled the event .hover() for the new item. 输入注释后,从服务器接收HTML并使用.append()将其插入DOM后,似乎不会处理新项目的.hover()事件。

There is the code: 有代码:

/**
 * This code manage insert comment with ajax
 **/

$(document).ready(function() 
{
    $('form[id^=insert_comment_for_product_]').submit(function (event)
    {
        event.preventDefault();

        var productId = $(this).attr('id');
        var productIdClear = productId.substr(productId.lastIndexOf('_', 0) - 1, productId.length);

        var textarea = $('#' + $(this).attr('id') + ' textarea').val();
        var textareaId = $('#' + $(this).attr('id') + ' textarea').attr('id');
        var token = $('#' + $(this).attr('id') + ' input#user_comment_product__token').val();

        var gif = $(this).parent('div').children('img.insert_comment_img');
        $(gif).show();

        $.post($(this).attr('action'),
        {
            'id': productIdClear.toString(),
            'user_comment_product[comment]': textarea,
            'user_comment_product[_token]' : token
        },
        function(data) 
        {
            $('div.product_comment>div').append(data);
            $('#' + textareaId).val(' ');
            $(gif).hide();
        });

    });
   /**
    * This is the function that no work afert .append()
    **/


    $('div.comment[id^=comment_]').hover(function()
    {
        var commentId = $(this).attr('id');

        $('#' + commentId + ' img.comment_delete').show();

        $('#' + commentId + ' img.comment_delete').click(function(event)
        {
            event.stopImmediatePropagation();
            commentId = commentId.substr(commentId.lastIndexOf('_') + 1, commentId.length);

            $.post("../../../user/comment/delete",
            {
                'id': commentId.toString()
            },
            function(data) 
            {
                if(data.responseCode == 200)
                {
                    $('div#comment_' + commentId).hide();
                }
            });
        })

    },
    function ()
    {
        var commentId = $(this).attr('id');

        $('#' + commentId + ' img.comment_delete').hide();
    });
});

WHY? 为什么?

You can use the on function to bind to elements that are dynamically added so instead of this: 您可以使用on函数绑定到动态添加的元素,而不是此:

$('div.comment[id^=comment_]').hover(function()

do this: 做这个:

$(document).on('mouseover', 'div.comment[id^=comment_]', function(e) {
    // code from mouseover hover function goes here
});

$(document).on('mouseout', 'div.comment[id^=comment_]', function(e) {
     // code from mouseout hover function goes here
});

.hover() is bound before your append happens, so the event isn't on the item. .hover()在您的追加发生之前就已绑定,因此该事件不在该项目上。 You need to use .on() for both mouseenter and mouseleave in order for it to work. 您需要对mouseenter和mouseleave都使用.on(),以使其正常工作。 See the 'Additional Notes' section on here: http://api.jquery.com/on/ 请参阅此处的“其他说明”部分: http : //api.jquery.com/on/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM