Determine jQuery toggle state -


I am using the toggle () method on the button so they have a Do something / return changes for example behavior:

  $ ('button'). Toggle (function () {// Case 1: Maximize ... ... another_function ($ (this));}, function () {// Case 2: minimize ... ... second_function ( $ (This));}); In both the  toggle  cases, the same task is finally called ( another_function () ). I do not want to manipulate the button in order to find out whether we are in  matter1  or  case2  (for example add a class, custom attribute set Before making the call function, and check those changes inside the function). 

Element is a way to detect state data, in which we are in the toggle case (which part of jQuery togo is known to execute somewhere to install a state)?

PS: There are already many posts related to that topic but they are about to hide / show the matter and their suggestions (check whether it appeared Is being hidden, ...) does not apply here.

You already know which state you are (max / at least) because that Special state code is being executed. You can simply pass this information as a code to another_function :

  $ ('button'). Toggle (function () {// Case 1: max / / ... another_function ($ (this), 'maximize')}}, function () {// Case 2: minimize ... other_function ($ (This), 'at least');};  

Update: jumble the internal state of jQuery

jQuery currently (1.7.2) .data () Back door uses to store the state for its toggle This back door is clearly marked as more than one private symbol in the source of jQuery and without saying that without any part of the posterior Subject to change of information. So I strongly recommend not going down this path .

He said, what's happening here:

jQuery uses a personal data space which can be found in the $ ._data () toggles To store the current click number, this Click Count module 2 decides which function is executed and increases on every click.

The key used to store this data is currently the string "lastToggle" + guid , where gyde is a value that is normal The form of guid is set on the toggle handler functions (using the global incremental counter) by jQuery automatically and you can get it after setting the toggle in this way:

  var f1 = function () {// case 1: maximize // ... another_function ($ (this), 'more In the '); }, F2 = function () {// case 2: at least // ... another_function ($ (this), 'minimize'); }; $ ('Button'). Toggle (F1, F2); // GUID is now available on both handlers: console.log (f1.guid, f2.guid);   

You can also set the value of guid to the first handler before calling it toggle:

< Pre> var f1 = function () {// Case 1: Maximize // ... another_function ($ (this), 'maximize'); }, F2 = function () {// case 2: at least // ... another_function ($ (this), 'minimize'); }; F1.guid = "foo"; $ ('Button'). Toggle (F1, F2); // GUID is now fixed on "foo" and available on both handlers: console.log (f1.guid, f2.guid);

Looking at this handle, you can still retrieve the number of clicks (and which way the handler will be called in the future)

 < Code> $ ._data $ ('button'), "last toggle" + good);  

Comments

Popular posts from this blog

java - NullPointerException for a 2d Array -

python - Assemble mpeg file unable to play in mediaplayer -

c# - NameSpace Manager or XsltContent to parse aspx page -