jquery - Hash location basics -
i'm trying understand how location.hash works in jquery, that, i'm trying begin basic form, , once right i'd go deeper, sadly i'm stuck @ think should simple thing.
here's code created modifying else's code found in post here:
$(document).ready(function(){ $("body").css("background-color", "#ff0"); $(window).bind( 'hashchange', function( event ) { if (window.location.hash == "red"){ $("body").addclass("red"); } else if (window.location.hash == "green") { $("body").addclass("green"); } event.preventdefault(); }); $(window).trigger("hashchange");
});
and here's page http://dlacrem.16mb.com/dlatest/hash.html
now, said, i'm trying learn there 80 mistakes in 10 lines :d but, shouldn't adding red class body when go hash.html#red?
i'm using bbq plugin ben alman
regards, , comes way!
window.location.hash includes hash symbol.
if (window.location.hash == "#red"){ $("body").addclass("red"); } else if (window.location.hash == "#green") { $("body").addclass("green"); }
additionally, inline-style set make body yellow override class (unless use !important, don't that!), you'll want make yellow in stylesheet rather inline.
you'll note once make red, green, stays green. because never remove classes, takes on 1 has highest specificity (green in case since last in stylesheet.) remedy this, you'll want remove other class.
Comments
Post a Comment