JavaScript / JQuery: use $ (this) in the variable name

I am writing a jquery plugin that changes the css value of certain elements to specific user actions. For other actions, the css value should be reset to their initial value.

Since I did not find a way to return the initial values ​​of css, I just created an array in which all the initial values ​​are stored at the beginning.

I did it with

var initialCSSValue = new Array()

pretty at the beginning of my plugin, and then, in some setup loop, where all my elements access, I used

initialCSSValue[$(this)] = parseInt($(this).css('<CSS-attribute>'));

This works very well in Firefox. However, I only found out that IE (even v8) again has problems accessing a specific value using

initialCSSValue[$(this)]

somewhere else in the code. I think this is due to the fact that I use the object ($ (this)) as the name of the variable.

?

+3
4

$(this).data()

, . jQuery Data , .

- ( <CSS-attribute> css):

$(this).data('initial-<CSS-attribute>', parseInt( $(this).css('<CSS-attribute>') ) );

:

$(this).data('initial-<CSS-attribute>');

: data:

, , data:

var saveCSS = function (el, css_attribute ) {
   var data = $(el).data('initial-css');
   if(!data) data = {};
   data[css_attribute] = $(el).css(css_attribute);
   $(el).data('initial-css', data);
}
var readCSS = function (el, css_attribute) {
   var data = $(el).data('initial-css');
   if(data && data[css_attribute])
     return data[css_attribute];
   else
     return "";
}
+2

jQuery . .

initialCSSValue[$(this).attr("id")] = parseInt...
+1

, , ...:)

CSS addClass removeClass - .

0

Source: https://habr.com/ru/post/1731900/


All Articles