English 中文(简体)
上一个和下个链接 如何从一个黑道列表中找到前一个和下个链接 hrefs?
原标题:How can I get the previous and next link hrefs from a list of hrefs?
  • 时间:2012-05-26 13:21:14
  •  标签:
  • jquery

I have the following code that checks for click events on links inside of the element with 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>
最佳回答

您可以使用诸如 :

$("#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 );
   }
});

"http://jsfiddle.net/mprabhat/ 34jj3/" rel="no follow" > fiddle

更新 :

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

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

""http://jsfiddle.net/mprabhat/UahMH/1/" rel="nown" >nd Fiddidd

问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签