javascript 如何从元素中删除可排序的 jQuery UI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12887581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:21:07  来源:igfitidea点击:

How to remove jQuery UI sortable from an element?

javascriptjqueryjquery-ui

提问by TK123

I am binding sortable to an element like so:

我将 sortable 绑定到一个元素,如下所示:

$('#elem').sortable({
   items: 'li',
   placeholder: 'drop-highlight',
   forcePlaceholderSize: true,
   revert: true
});

On a certain event I want to no longer allow this element to be sortable. How do I unbind it?

在某个事件中,我不想再允许此元素可排序。我该如何解绑?

All these were tried individually and all failed (i.e. #elem was still sortable afterwards):

所有这些都单独尝试过,但都失败了(即 #elem 之后仍然可以排序):

$('#elem').unbind('sort');
$('#elem').sortable('destroy');
$('#elem').sortable('option', 'sort', null);

回答by davidkonrad

Yeah, it su**s :-( this removes the functionality andthe css classes / injected spans after 1 sec (destroy appearently just kill the widget, not the added classes or tags)

是的,它 su**s :-( 这会在 1 秒后删除功能css 类/注入的跨度(破坏似乎只是杀死小部件,而不是添加的类或标签)

$('#elem').sortable({
   items: 'li',
   placeholder: 'drop-highlight',
   forcePlaceholderSize: true,
   revert: true
});

place this code in a handler when you need to "unsortable" the list

当您需要“不可排序”列表时,将此代码放在处理程序中

function() {
    $("#elem").sortable("destroy"); //call widget-function destroy
    $("#elem li").removeClass('ui-state-default');
    $("#elem li span").remove();
}

$("#elem").sortable("destroy"); doesactually destroy the sortable functionality, it just forget to clean up.

$("#elem").sortable("destroy"); 实际上破坏排序的功能,它只是忘了清理。

回答by Vishnu Prasanth G

If you want to disable the sortable then try this:

如果要禁用可排序,请尝试以下操作:

$("#elem").sortable("disable");

and to enable back

并启用回

$("#elem").sortable("enable" );