开发者

When i append html element it override click functions

开发者 https://www.devze.com 2023-04-06 21:44 出处:网络
this.div=$j(\"<div class=\'test\' id=\'test\"+this._x+\"\'>\"+this.getHtml(inner)+\"<a id=\'testa\"+this._x+\"\'\'>Close</a></div>\");
this.div=$j("<div class='test' id='test"+this._x+"'>"+this.getHtml(inner)+"<a id='testa"+this._x+"''>Close</a></div>");
    this.div.hide().appendTo($j("body")).fadeIn(100);
    this.div.children("#testa"+this._x).bind('click', function(){
          alert(this._x);
   });

in constructor

开发者_如何学编程this._x = x;
x++;

when i click testa he gives me this._x last x number is there an override?


Yes, JS events are tied to the element itself. If you delete and recreate an element the handlers go with it. You can either re-connect them, or look into using jQuery's .live() functionality.


this._x in your event listener points to non existant property of (A._x).

this.div=$j("<div class='test' id='test"+this._x+"'>"+this.getHtml(inner)+"<a id='testa"+this._x+"''>Close</a></div>");
    this.div.hide().appendTo($j("body")).fadeIn(100);
    var newX = this._x;
    this.div.children("#testa"+this._x).bind('click', function(){
          alert(newX);
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号