how to kill/reset a running jquery plugin?

拜拜、爱过 提交于 2019-12-24 08:55:58

问题


I have a slideshow plugin which works fine. Alone...

I have 3 different buttons on one page. They all call the same jQuery slideshow plugin and load the slideshow with different images into the same divider (with id="slideshow")

I remove the slideshow and it's elements from the #slideshow every single time before I load the new slideshow with the new images.

It would all work fine except that the more times I click on the buttons the messier the whole thing gets because the plugin is opened every single time and the plugin gets confused which images to show.

My question is. How can I completely kill a running plugin so the new one can load from the beginning. I tried empty(), die(), remove(), detach() APIs without successs.

Is there any other way?

Thanks.

http://muttley.freewebspace.com/slideshow/


回答1:


Unfortunately there isn't a common way to destroy plugins on an element, if the plugin itself doesn't support it. You will probably have to remove the element you actually called the plugin on and create it again:

var oldDiv = $('#slideshow');
var parent = oldDiv.parent();
oldDiv.remove();
parent.append($('<div>').attr('id', 'slideshow'));
// Initialize slideshow here again

// Untested but shorter way:
$('#slideshow').replaceWith($('<div>').attr('id', 'slideshow'));



回答2:


It all depends on the plugin. Does this plugin support multiple slideshows on a page? If so, instead of removing the contents of #slideshow, perhaps it will work bette rif you replace #slideshow itself with a new element.



来源:https://stackoverflow.com/questions/7122840/how-to-kill-reset-a-running-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!