开发者

how to attach and detach a plugin method

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();
  }
}


Don't use unbind; use the "destroy" command instead.

For instance:

function abc(){
  if (/* condition */) {
    $(".container").accordion();
  } else {   
    $(".container").accordion('destroy');
  }
}

See the docs. It may be more appropriate to use the enable and disable commands instead, depending on your circumstances.


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...

function abc(){
  if (true) {
    $.getScript("accordion.js", function(){
    alert("reinitialized");
    });
    $(".container").accordion();
  } else {   
    $(".container").unbind();
  }
}


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

function abc(){
  if (!$(".container").hasClass('some_accordion_class')) {
    $(".container").accordion();
  } else {   
    $(".container").destroy();
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜