Home>Article>Web Front-end> What to do if toggle cannot be used in jQuery
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.
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 ); }
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!