简体   繁体   English

当我附加html元素时,它将覆盖click函数

[英]When i append html element it override click functions

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? 当我单击testa时,他给了我this._x最后一个x数字是否存在替代?

Yes, JS events are tied to the element itself. 是的,JS事件与元素本身相关。 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. 您可以重新连接它们,也可以使用jQuery的.live()功能进行研究。

this._x in your event listener points to non existant property of (A._x). 您的事件侦听器中的this._x指向(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);
   });

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

相关问题 jQuery:如何覆盖特定元素的默认方法(即html,append)? - jQuery: how to override default methods (i.e html, append) for particular element? 单击同一元素时如何触发不同的功能? - How to trigger different functions when I click the same element? 如何使用循环在单击时将克隆的html元素附加到父元素? - How do I append cloned html elements to parent element on click using a loop? 如何覆盖自定义html元素的onclick事件,使其仅在我要求时才分派? - How do I override onclick event for a custom html element so it only dispatches when I ask it to? 定位功能中动态创建的HTML元素(用于追加事件-不能单击!)jQuery - Target dynamically created HTML element in function (for append event— not click!) jQuery 如何将v-on:click事件附加到html元素 - How to append v-on:click event to html element 在ng-click上将自定义AngularJS指令附加到容器HTML元素 - Append a custom AngularJS directive on ng-click to a container HTML element 当我单击使用附加的服务器端html编码中的第一个div时如何隐藏和显示另一个div - How to hide and show another div when I click first div in server side html coding using append .append()一个元素,单击()不注册 - .append() an element, click() not registering 当我使用$ .append时如何克隆元素 - How to clone element when i use $.append
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM