What to do if toggle cannot be used in jQuery

藏色散人
Release: 2023-02-09 10:25:17
Original
2305 people have browsed it

The solution to the problem that toggle cannot be used in jQuery: 1. Rewrite part of the source code into a plug-in; 2. Introduce the plug-in into the web page, and then pass "$.fn.toggle = function( fn, fn2 ) { var args = arguments,guid = fn.guid || $.guid ,i=0,toggler = function( event ) {...}" can be used with toggle.

What to do if toggle cannot be used in jQuery

The operating environment of this tutorial: Windows 10 system, jquery version 1.9, DELL G3 computer

What to do if toggle cannot be used in jQuery ?

The solution to the failure of toggle() after version 1.9 of jQuery

Recently, I discovered that the toggle method of the higher version of jQuery is invalid. I found the solution before The source code of the version implements the toggle function, and I have seen many senior blogs and learned a lot...

First of all, when encountering this situation, we should first check the source code of the previous version for research, and try Just rewrite that part of the source code into a plug-in and introduce the plug-in into your own web page for long-term use.

$.fn.toggle = function( fn, fn2 ) {
    var args = arguments,guid = fn.guid || $.guid++,i=0,
    toggler = function( event ) {
      var lastToggle = ( $._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
      $._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
      event.preventDefault();
      return args[ lastToggle ].apply( this, arguments ) || false;
    };
    toggler.guid = guid;
    while ( i < args.length ) {
      args[ i++ ].guid = guid;
    }
    return this.click( toggler );
}
Copy after login

What to do if toggle cannot be used in jQuery

Recommended learning: "jQuery Video Tutorial"

The above is the detailed content of What to do if toggle cannot be used in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template