1

1)我们有一个index.html带有块的页面:

<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)其他带有链接index.html和一些目标的页面:

<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/>

问题是如何捕获页面上的当前链接目标index.html并为目标块提供额外的类。

如果打开页面的当前链接是index.html#action1,那么添加类.current<div id="action1">One</div>- 将变为<div id="action1" class="current">One</div>

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

等等。

  • 查看 #target
  • 查看id
  • if target= idaddClass("current") 用于块id

谢谢。

4

1 回答 1

3

你可以这样做:

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

这使用位置的hash属性(如果有的话),它会"#action1"作为一个ID 选择器然后通过添加类.addClass(),简短而简单:)

于 2010-06-11T13:43:54.357 回答