javascript - Jquery - After .append, .hover on other element not working - Stack Overflow

I wrote a bit of code to handle the insertion of a ment via AJAX.Once you have entered the ment, havin

I wrote a bit of code to handle the insertion of a ment via AJAX. Once you have entered the ment, 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.

There is the code:

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

$(document).ready(function() 
{
    $('form[id^=insert_ment_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_ment_product__token').val();

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

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

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


    $('divment[id^=ment_]').hover(function()
    {
        var mentId = $(this).attr('id');

        $('#' + mentId + ' imgment_delete').show();

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

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

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

        $('#' + mentId + ' imgment_delete').hide();
    });
});

WHY?

I wrote a bit of code to handle the insertion of a ment via AJAX. Once you have entered the ment, 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.

There is the code:

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

$(document).ready(function() 
{
    $('form[id^=insert_ment_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_ment_product__token').val();

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

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

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


    $('div.ment[id^=ment_]').hover(function()
    {
        var mentId = $(this).attr('id');

        $('#' + mentId + ' img.ment_delete').show();

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

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

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

        $('#' + mentId + ' img.ment_delete').hide();
    });
});

WHY?

Share asked May 23, 2013 at 20:54 Angelo GiuffrediAngelo Giuffredi 9433 gold badges13 silver badges27 bronze badges 2
  • 1 Why? Because $('div.ment[id^=ment_]').hover(... only binds a hover handler to elements matching that selector at that moment, not for elements that might match it in future. – nnnnnn Commented May 23, 2013 at 20:55
  • @nnnnnn How i can match $('div.ment[id^=ment_]').hover(... after .append()? – Angelo Giuffredi Commented May 23, 2013 at 21:10
Add a ment  | 

2 Answers 2

Reset to default 6

You can use the on function to bind to elements that are dynamically added so instead of this:

$('div.ment[id^=ment_]').hover(function()

do this:

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

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

.hover() is bound before your append happens, so the event isn't on the item. You need to use .on() for both mouseenter and mouseleave in order for it to work. See the 'Additional Notes' section on here: http://api.jquery./on/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742260783a4410863.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信