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") for ブロックid

ありがとう。

4

1 に答える 1

3

あなたはこれを行うことができます:

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

これは場所のhashプロパティを使用します(存在する場合)、IDセレクター"#action1"として使用され、短くてシンプルな方法でクラスが追加されます:).addClass()

于 2010-06-11T13:43:54.357 に答える