如何附加和分离插件方法
我正在尝试根据外部函数返回的 true 或 false 分离然后将 jquery 插件方法重新附加到 div。使用 unbind 我可以将 Accordion() 方法与 div 分离,但是我无法将 Accordion 方法再次重新绑定到 div。一旦执行 unbind 方法,即使条件返回 true,“$('.container').accordion()”代码也不起作用。请建议取消绑定插件方法然后再次绑定它的正确方法。
以下是供您参考的示例代码
- HTML:
<div class="container" onmousedown="abc()">
Javascript:
function abc(){
if (true) {
$(".container").accordion();
} else {
$(".container").unbind();
}
}
i am trying to detach and then reattach a jquery plugin method to a div based on true or false returned by an external function. Using unbind i am able to detach the accordion() method from the div, however i am unable to rebind the accordion method again to the div. Once the unbind method is executed then even if the condition return true, "$('.container').accordion()" code doesn't work. please suggest the correct way to unbind a plugin method and then bind it again.
Below is the sample code for your reference-
HTML:
<div class="container" onmousedown="abc()">
Javascript:
function abc(){
if (true) {
$(".container").accordion();
} else {
$(".container").unbind();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用
unbind
;请改用“销毁”命令。例如:
请参阅文档。根据您的具体情况,使用
enable
和disable
命令可能更合适。Don't use
unbind
; use the "destroy" command instead.For instance:
See the docs. It may be more appropriate to use the
enable
anddisable
commands instead, depending on your circumstances.这是因为一旦你解除了手风琴的绑定,它就没有那个方法了...所以你需要再次绑定它...你能做的就是每次调用你的手风琴js...
it's because once you unbind the accordion it's doesn't have that method...so you need to bind it again...what you can do is call your accordion js every time...
我没有直接的答案,但似乎有线索。
您应该检查什么类被添加到您的 div 中,然后您在其上应用 Accordion() 。 Firebug 可以很容易地看到它。既然很清楚,代码将是
I don't have direct answer, but seems got a clue.
You should check, what class is being added to your div, then you apply accordion() pluing on that. It could be easy see by Firebug. Since it is clear, code would be