jquery 克隆 +实时问题
我有一段 html,我想在新复制的块特定处理程序中复制并分配特定选择。我的问题是,当我复制这个块时,处理程序被触发n次。有什么建议来调试这个麻烦吗? (总列表:总列表)
Jquery 代码:
"appendNewBlockTo" : function(oAppendTo, sFilterByCName){
var iIndex = $("." + sFilterByCName).length/2;
var oNewPlayerCS = $("#" + this.sTemplateBlockID).clone(false);
// alter template before copying
$(oNewPlayerCS).find("." + this.sClassListboxCName).addClass(sFilterByCName).attr('index', iIndex).live($.browser.msie ? 'click' : 'change', function(){PlayerCS.enableSListbox(sFilterByCName, iIndex);});
$(oNewPlayerCS).find("." + this.sSpecListboxCName).addClass(sFilterByCName).attr('index', iIndex);
$(oNewPlayerCS.removeAttr('id').removeAttr('style')).appendTo(oAppendTo);
},
要复制多次的 HTML 块:
<div id="playerCSTemplate" style="display:none;">
<select class="class_listbox">
<option selected="selected" value="0">Class</option>
<option class="class_value" value="1">mage</option>
<option class="class_value" value="2">priest</option>
</select>
<select class="spec_listbox" disabled>
<option value="0">Unknown</option>
<option class="spec_class class_specs_1">Frost</option>
<option class="spec_class class_specs_1">Fire</option>
<option class="spec_class class_specs_2">Holy</option>
<option class="spec_class class_specs_2">Shadow</option>
</select>
</div>
I have a piece of html that I'd like to copy and assign particular selects in a new copied chunk particular hanlders. My problem is that handler is being triggered n times I copy this chunk. Any suggestions to debug this trouble ? (total listing: total listing)
Jquery code:
"appendNewBlockTo" : function(oAppendTo, sFilterByCName){
var iIndex = $("." + sFilterByCName).length/2;
var oNewPlayerCS = $("#" + this.sTemplateBlockID).clone(false);
// alter template before copying
$(oNewPlayerCS).find("." + this.sClassListboxCName).addClass(sFilterByCName).attr('index', iIndex).live($.browser.msie ? 'click' : 'change', function(){PlayerCS.enableSListbox(sFilterByCName, iIndex);});
$(oNewPlayerCS).find("." + this.sSpecListboxCName).addClass(sFilterByCName).attr('index', iIndex);
$(oNewPlayerCS.removeAttr('id').removeAttr('style')).appendTo(oAppendTo);
},
HTML chunk to be copied many times:
<div id="playerCSTemplate" style="display:none;">
<select class="class_listbox">
<option selected="selected" value="0">Class</option>
<option class="class_value" value="1">mage</option>
<option class="class_value" value="2">priest</option>
</select>
<select class="spec_listbox" disabled>
<option value="0">Unknown</option>
<option class="spec_class class_specs_1">Frost</option>
<option class="spec_class class_specs_1">Fire</option>
<option class="spec_class class_specs_2">Holy</option>
<option class="spec_class class_specs_2">Shadow</option>
</select>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
clone(true)
- true 表示应该克隆元素以及它们在其上设置的事件。然后,您向克隆添加更多事件,从而产生重复。将该行更改为:
或者,如果需要,您可以省略
false
,因为它是默认值。或者,您可以保留
clone(true)
并且不向其中添加事件。You are using
clone(true)
- the true indicates that the elements should be cloned, along with the events they have set on them. You are then adding more events to the clones, hence the duplication.Change the line to:
Or you can omit the
false
if required, as it is the default anwyay.Alternatively, you can leave the
clone(true)
and simply not add the events to it.