开发者

jQuery check link

1) We have a page index.html with blocks:

<body>
<div id="action1">One</div>
<div id="action2">Two</div>
<div id="action3">Three</div>
</body>

2) CSS

body div { color: blue; }
body div.current { font-weight: bold; color: red; }

3) Other pages with links to index.html and some target:

<a href="index.html#action1">Link to One</a><br/>
<a href="index.html#action2">Link to Two</a><br/>
<a href="index.html#action3">Link to Three</a><br/>

The question is how to catch current link target on page index.html and give extra class for target block.

If current link of opened page is index.html#action1, then add class .current to <div id="action1">One</div>- will become <div id="action1" class="current">One</div>

If index.html#action2-> <div id="action2" class="current">Two</div>

And so on.

Thanks.


You can do this:

$(function() {
  if(location.hash) $(location.hash).addClass("current");
});

This uses the location's hash property (if there is one), it'd be "#action1" as an ID selector then adds the class via .addClass(), short and simple :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜