[英]My New HTML Elements Are Being Created At An Exponential Rate WTF Javascript And jQuery
Hope you are all doing well. 希望你们都做得很好。 I seem to have a bit of a problem with my code and I was hoping you'd be able to assist.
我的代码似乎有点问题,我希望你能够提供帮助。 Every time I run the contmgr_create_media in conjunction with the contmgr_update_order functions as I do in with the add image button, I get new items created but in an exponential fashion (1, 2, 4, 8, 16).
每次我像使用添加图像按钮一样运行contmgr_create_media和contmgr_update_order函数时,我会以指数方式创建新项目(1,2,4,8,16)。 If I do not fire the contmgr_update_order function when the add image button is clicked it works as it should.
如果我在单击添加图像按钮时不激活contmgr_update_order函数,它将按预期工作。 Any suggestions?
有什么建议么? Thanks for your time and the code is below.
感谢您的时间,代码如下。
Code:
jQuery(function() {
contmgr_update_order();
jQuery('#contmgr-sortable').sortable({
update: function(event, ui) { contmgr_update_order(); }
});
jQuery('#contmgr-sortable').disableSelection();
//this function deletes media elements by using the jQuery commant .remove
function contmgr_delete_media() {
jQuery('.contmgr-delete').click( function () {
jQuery(this).parents('.ui-state-default').remove();
contmgr_update_order();
});
};
//this function updates the feedback for the user as to how the media elements are ordered
//it also inserts / updates an id for each element ensuring that that specific element can
//be called at a later point in time
function contmgr_update_order(){
jQuery('.contmgr-media-element').each( function(index) {
jQuery(this).find('.contmgr-media-order').html(index);
jQuery(this).find('.contmgr-media-order').attr('id', index);
});
var new_media_element = '<li class="ui-state-default contmgr-media-element"><span class="contmgr-media-title-span"><div class="contmgr-media-title">Really Long Title</div></span><div class="contmgr-media-content"><span class="contmgr-media-order"></span><span class="contmgr-media-control-span"><a class="button contmgr-media-buttons">Edit</a><a class="button contmgr-media-buttons contmgr-delete">Delete</a></span></div></li>';
function contmgr_create_media(){
jQuery('#contmgr-sortable').append(new_media_element);
};
//this function fires here to ensure that all of the delete buttons on the media elements
//are functioning and listening for that click
contmgr_delete_media();
jQuery('#contmgr-add-image').click( function () {
contmgr_create_media();
contmgr_update_order();
contmgr_delete_media();
});
};
});
Every time you're calling 每次你打电话
jQuery('#contmgr-add-image').click( function () {
contmgr_create_media();
contmgr_update_order();
contmgr_delete_media();
});
You bind another trigger. 你绑定另一个触发器。 Try to unbind click before
.click()
, or try to use .live('click', function(e) {})
. 尝试在
.click()
之前取消绑定,或尝试使用.live('click', function(e) {})
。 If you use second, any new element with given selector will automatically with click
trigger. 如果使用秒,则具有给定选择器的任何新元素将自动带有
click
触发器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.