4

我有以下三个项目并排显示:

<div class="page-header">
    <h1 style="line-height:0">Title</h1>
        <ul style="float: right; list-style-type: none;">
    <li><a href="http://test1.com">T1</a></li>
    <li><a href="http://test2.com">T2</a></li>
    <li><a href="#">T3</a></li>
        </ul>
</div>

一旦用户单击它(例如T1),我想突出显示该项目。类似于 stackoverflow 的问题、标签、用户等块。

4

1 回答 1

4

看一下这个:

// Start up jQuery
$(document).ready(function() {
  $('a.nav').click(function(e) {
    e.preventDefault();
    
    $('a.nav').removeClass('current_page');
    
    // Do an ajax call instead
    $('#response').html($(this).attr("href"));

    $(this).addClass('current_page');
  });
});
* {
  font-family: Verdana;
  font-size: 13px;
}
div#wrapper {
  margin: 20px;
}
.current_page {
  color: red;
  font-weight: bold;
  text-decoration: none;
}
#response {
  width: 300px;
  height: 200px;
  padding: 5px;
  border: 1px dotted #777;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <a class="nav" href="http://www.google.com">Google</a> |
  <a class="nav" href="http://www.facebook.com">Facebook</a> |
  <a class="nav" href="http://www.stackoverflow.com">Stackoverflow</a> |
  <a class="nav" href="http://www.myspace.com">Myspace</a> |
  <a class="nav" href="http://www.craigslist.com">Craigslist</a>

  <p>&nbsp;</p>
  <p>Response:</p>

  <div id="response"></div>
</div>

所以基本上,它将一个类推.current_page送到激活的锚标记,该锚标记在 DOM 中印记该 MEMORY ID。然后,您将实现一个 ajax 调用以在页面中注入内容。

于 2012-06-19T22:01:00.537 回答