本文介绍了添加元素后重新初始化jQuery UI droppable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些页面加载的元素是可放置的。在某些方面,我添加了更多这些元素(相同的选择器),我希望新元素也可以放置。是唯一一个使用所有选项调用原始droppable函数的选项吗?你看,droppable函数调用非常大,我真的不想以这种方式复制代码。有更优雅的方式吗?

I have some elements that on page load are droppable. At some points I add some more of these elements (same selector) and I want the new elements to be droppable as well. Is the only option to call the original droppable function with all the options? You see, The droppable function call is pretty big, and I don't really want to duplicate code this way. Is there a more elegant way?

这是澄清的代码:

$('td:not(.td-disabled)').droppable({
        hoverClass: "ui-state-active",
        drop: function( event, ui ) {
            // ... very big block of code
        }
    });
$('.td-disabled').removeClass('td-disabled');

// I dont want to do this:
$('td').droppable({
            hoverClass: "ui-state-active",
            drop: function( event, ui ) {
                // ... very big block of code
            }
        });


推荐答案

您可以尝试:

var hoverCls = 'ui-state-active';
var dropFn = function(event, ui) {

};

$('td').droppable('destroy').droppable({
   hoverClass: hoverCls,
   drop: dropFn
});

这篇关于添加元素后重新初始化jQuery UI droppable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 22:30