javascript - Add class if hash value exists in page URL -
i'm trying add class div if hash value exists in url:
<script type="text/javascript"> if(window.location.hash) { alert(window.location.hash); $(".pinit").addclass("pinned"); $(".hideit").addclass("hidden"); $(".changeit").addclass("changed"); } else { $(".pinit").removeclass("pinned"); $(".hideit").removeclass("hidden"); $(".changeit").removeclass("changed"); } </script>
html:
<header class="changeit"> </header> <div class="heading"> <h1 class="hideit">test</h1> <h2 class="pinit"> </h2> </div>
my script show alert not add class. problem?
the problem code being run before pinit etc exist. need wrap it:
$(function(){ if(window.location.hash) { alert(window.location.hash); $(".pinit").addclass("pinned"); $(".hideit").addclass("hidden"); $(".changeit").addclass("changed"); } else { $(".pinit").removeclass("pinned"); $(".hideit").removeclass("hidden"); $(".changeit").removeclass("changed"); } });
more info jquery here
Comments
Post a Comment