0

我有以下代码检查带有 id = menu 的元素内的链接上的点击事件:

$("#menu")
   .on('click', 'a[href^="/City"]', function (event) {
   event.preventDefault();
   $('#editLink').attr("data-href", link);
});

此代码适用的 HTML 如下所示:

<ul id="menu">
   <li><a href="/City/0101004H">1</a></li>
   <li><a href="/City/0101004I">2</a></li>
   <li><a href="/City/0101004J">3</a></li>
   <li><a href="/City/0101004K">4</a></li>
</ul>

我在屏幕的另一部分有两个链接,如下所示:

<a id="prev" href="#">Prev</a>
<a id="next" href="#">Next</a>

我怎样才能做到这一点,如果用户点击“2”链接,那么这些链接将更改为:

<a id="prev"  href="/City/0101004H" title="City 1">Prev</a>
<a id="next"  href="/City/0101004J" title="City 3">Next</a>
4

1 回答 1

2

你可以使用 do like :

$("#menu").on('click', 'a[href^="/City"]', function (event) {
   event.preventDefault();
   if(jQuery(this).text() == 2) { // this check is not required if this functionality is required for any 'a' under #menu
      jQuery(this).closest('ul').find('li:first').find('a').css('color','yellow');
      jQuery(this).closest('ul').find('li:last').find('a').css('color','pink');
      $('#prev').prop('href', $(this).parent().prev().find('a').prop('href')).prop('title','City 1').css('color','red');
      $('#next').prop('href', $(this).parent().next().find('a').prop('href')).prop('title','City 3').css('color','green');
   }
});

小提琴

更新:

jQuery(this).parent().prev().find('a') // this will give prev a 

jQuery(this).parent().prev().find('a') // this will give next a

第二小提琴

于 2012-05-26T13:27:13.847 回答