简体   繁体   English

Ajax更新后,新的dom元素未连接事件处理程序(Internet Explorer)

[英]Event handler not wired after Ajax Update, new dom element (Internet Explorer)

Problem: New Inserted Dom elements aren't been wired correctly, function deletepost not firing. 问题:新插入的Dom元素未正确连接,功能deletepost无法触发。 This happens only on IE and for new elements only added to DOM. 这仅在IE上发生,并且仅在DOM中添加了新元素。

$(function(){
        $('#postentry').submit(function() {
          var tyu = $('#tittle_ent').val();
          if(tyu.length <= 2)
            {alert('Enter More Text!'); return false;}else
            {
                $.ajax({
                    type:'post',
                    url: '/posts_in.php',
                    dataType: "json",
                    data: $("#postentry").serialize(),
                    success:function(data){
                        var tittle = data[0];
                        var id= data[1];                        
                        $('<div></div>').attr('id','post'+id).addClass('boxee').html(tittle).prependTo('#boxer');                        
                        $('<img src="img/page-text-delete-icon.png" name="'+id+'">').attr({id:'postchk'+id,onclick: 'deletepost(this.name);'}).appendTo('#post'+id);                        
                        $('#tittle_ent').val('').focus();

                    }
                });
                return false;
            }
        });
    });

Use jquery live 实时使用jQuery

$(function(){
        $("#boxer img").live("click", function(){
          deletepost($(this).attr("name"));
        });

        $('#postentry').submit(function() {
          var tyu = $('#tittle_ent').val();
          if(tyu.length <= 2)
            {alert('Enter More Text!'); return false;}else
            {
                $.ajax({
                    type:'post',
                    url: '/posts_in.php',
                    dataType: "json",
                    data: $("#postentry").serialize(),
                    success:function(data){
                        var tittle = data[0];
                        var id= data[1];                        
                        $('<div></div>').attr('id','post'+id).addClass('boxee').html(tittle).prependTo('#boxer');                        
                        $('<img src="img/page-text-delete-icon.png" name="'+id+'">').attr({id:'postchk'+id,onclick: 'deletepost(this.name);'}).appendTo('#post'+id);                        
                        $('#tittle_ent').val('').focus();

                    }
                });
                return false;
            }
        });
    });

'onclick' is ancient/oldschool stuff, especially when using jquery. “ onclick”是古老/古老的东西,尤其是在使用jquery时。 Try this variant instead: 请尝试以下变体:

$('<img src="img/page-text-delete-icon.png" name="'+id+'">')
    .attr('id', 'postchk'+id)
    .click(function () { deletepost(this.name); })   /// use .click() instead.
    .appendTo('#post'+id);                        

是的,您必须使用jQuery live()函数,有关更多详细示例,请参见http://developerfaq.wordpress.com/2011/07/28/fire-event-on-dynamically-generation-dom-element-with -jquery /

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

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