Home > Web Front-end > JS Tutorial > body text

Solution to toggle() hidden problem_jquery

WBOY
Release: 2016-05-16 16:59:57
Original
1085 people have browsed it

When I wrote an example recently, I used the toggle function, but the element would be hidden when called. Previously, it only switched between multiple events in turn. I was puzzled, so I searched online to view the jQuery API documentation. Finally found the reason:
It turns out that toggle() has changed after jQuery 1.9. The following are the Notes from the official website:
Note: This method signature was deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation methodnamed .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of argumentspassed.
In earlier versions, there were two toggle() with the same name, but the execution The method is different:
.toggle( handler(eventObject), handler(eventObject) [, handler(eventObject) ] )
Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.
============================================ =========
.toggle( [duration ] [, complete ] )
Description: Display or hide the matched elements.
And later versions replace the first toggle() function It was removed, which caused the element to be hidden when it was used to call the switching function.
========================
Since this function has been removed, it is still needed to implement the requirements. How to realize the switching of multiple events in turn?
It can be triggered by judging different situations through the click event, or by setting a variable to count the number of clicks to execute different functions.

Copy code The code is as follows:

var num=0;
$('# button').click(function(e){
if(num %2 == 0){
//doSomething
}else{
//doOtherSomething
}
e .preventDefault(); //Prevent the default action of the element (if it exists)
});
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