gpt4 book ai didi

javascript - 我的新 HTML 元素正在以指数速度创建 WTF Javascript 和 jQuery

转载 作者:行者123 更新时间:2023-12-02 19:52:02 25 4
gpt4 key购买 nike

希望你们一切都好。我的代码似乎有点问题,希望您能够提供帮助。每次我将 contmgr_create_media 与 contmgr_update_order 函数结合使用(就像使用“添加图像”按钮一样)时,都会以指数方式创建新项目(1、2、4、8、16)。如果我在单击“添加图像”按钮时不触发 contmgr_update_order 函数,它将按预期工作。有什么建议么?感谢您的宝贵时间,代码如下。

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();
});
};

});

最佳答案

每次你打电话

jQuery('#contmgr-add-image').click( function () {
contmgr_create_media();
contmgr_update_order();
contmgr_delete_media();
});

您绑定(bind)了另一个触发器。尝试在.click()之前取消绑定(bind)click,或者尝试使用.live('click', function(e) {})。如果您使用第二个,任何具有给定选择器的新元素将自动使用 click 触发器。

关于javascript - 我的新 HTML 元素正在以指数速度创建 WTF Javascript 和 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9147667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com