Does something like jQuery.toggle(boolean) exist?

前端 未结 4 1603
情深已故
情深已故 2020-12-08 12:47

I write something similar to the following code a lot. It basically toggles an element based on some condition.

In the following made-up example, the condition is \"

4条回答
  •  离开以前
    2020-12-08 13:27

    If toggle() is not good for you (e.g. because it animates), you can write a small jQuery plugin, like this:

    $.fn.toggleIf = function(showOrHide) {
      return this.each(function() {
        if (showOrHide) {
          return $(this).show();
        } else {
          return $(this).hide();
        }
      });
    };
    

    and then use it like this:

    $(element).toggleIf(condition);
    

提交回复
热议问题