jquery 从元素中删除插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3205920/
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-08-26 15:01:15  来源:igfitidea点击:

jquery remove plugin from element

jqueryplugins

提问by Steven Cheng

I've got something similar to the following code:

我有类似于以下代码的内容:

$(a).click(function() {
  $(element).plugin();
});

Is there a way to remove a plugin from an element other than using $($.plugin).remove()? Not sure if I have the terminology correct but basically I want to reset the element to it's original state.

除了使用之外,有没有办法从元素中删除插件$($.plugin).remove()?不确定我的术语是否正确,但基本上我想将元素重置为其原始状态。

Thanks

谢谢

采纳答案by tvanfosson

You'd have to know what the plugin does in order to reverse it's effects. Many plugins add extra elements to the DOM, handlers to elements, etc. If the plugin doesn't create any extra elements, you might be able to simply do a clone(without data and events) and replace or even just unbindall event handlers, but that's not always going to be effective. It would be very dependent on the plugin in question.

你必须知道插件做了什么才能逆转它的效果。许多插件向 DOM 添加了额外的元素、元素的处理程序等。如果插件没有创建任何额外的元素,您可以简单地进行克隆(没有数据和事件)并替换甚至只是取消绑定所有事件处理程序,但这并不总是有效。这将非常依赖于相关插件。

回答by devside

Here is my dirty solution:

这是我的肮脏解决方案:

$('#myWidget,#myWidget *').unbind().removeData();

回答by Matchu

Unless the plugin provides that functionality, it probably can not be done that easily. You'll have to investigate what the plugin does, and specifically undo those things - or store a clone of the non-plugged-in element to replace it with later.

除非插件提供该功能,否则它可能无法那么容易地完成。您必须调查插件的作用,并专门撤消这些事情 - 或存储非插入元素的克隆以供以后替换。