javascript - Unexpected behavior with class added by jQuery -


a tale of 2 fiddles (please use run button after jsfiddle pages load clearer idea of happeneing).

the first fiddle

dead simple:

$("body").addclass("noscroll"); alert($("body").hasclass("noscroll")); $("body").removeclass("noscroll"); alert($("body").hasclass("noscroll")); 

with css:

.noscroll {     background-color: pink;     position: fixed;     width: 100%;     top: 200px; } 

we have class. class added body, changing body's appearance/behavior. class removed body , body reverts default. working expected.

the second fiddle

$("body").addclass("noscroll"); alert($("body").hasclass("noscroll")); $(".noscroll").css({     "background-color" : "pink",     "position"  : "fixed",     "width"     : "100%",     "top"       : "200px" }); $("body").removeclass("noscroll"); alert($("body").hasclass("noscroll")); 

no accompanying css time, it's added jquery, otherwise pretty similar above. working point. css applied, isn't removed. why happening?

thanks!

for second fiddle, when call css() on noscroll selector, applies styles inline element class noscroll. however, styles not preserved in named css style.

so code working. adding class noscroll, no styles affiliated class in css. also, removing class, styles css() call stay because applied inline.

to better idea, see fiddle inline style removed manually @ end.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -