English 中文(简体)
上一个和下个链接 如何从一个黑道列表中找到前一个和下个链接 hrefs?
原标题:How can I get the previous and next link hrefs from a list of hrefs?
  • 时间:2012-05-26 13:15:46
  •  标签:
  • 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 class="controls-buttons question-grid" id="menu">
   <li class="question-grid"><a href="/City/0101004H">1</a></li>
   <li class="question-grid"><a href="/City/0101004I">2</a></li>
   <li class="question-grid"><a href="/City/0101004J">3</a></li>
   <li class="question-grid"><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>

如果用户点击链接更改为“ 1” 的“ 1 ” :

<a id="prev"  href="#"></a>
<a id="next"  href="/City/0101004J" title="City 2">Next</a>

如果用户点击列表中最后一个元素,链接将被修改为 :

<a id="prev"  href="/City/0101004H" title="City 3">Prev</a>
<a id="next"  href="#"></a>

对我而言,最大的问题是如何检查当前链接之前或之后是否有链接。 我知道基本的jQuery, 但我希望有人能帮助我提出一些建议。

问题回答

我猜是这样的:

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

$( #prev, #next ).on( click , function() {
    var isNext = this.id=== next ,
        liNow = $( #editLink ).attr("data-href") || $( #menu li:first a ).attr( href ),
        nextA = $( a[href=" +liNow+ "] ,  #menu ).parent( li )[isNext? next : prev ]( li ).find( a );
    if (!nextA.length) nextA = isNext ? $( #menu li:last a ) : $( #menu li:first a );
    nextA.trigger( click );
});

FIDDLE

我不测试这个,但希望 有用:

$( .question-grid a ).click(function(){

   var number=parseInt($(this).html());

   if(number<2)
   {
      $( #prev ).attr( href , # );
      $( #next ).attr( href ,$(this).next( a ).attr( href );
   }else if(number>1 && number<4){
      $( #prev ).attr( href ,$(this).prev( a ).attr( href ));
      $( #next ).attr( href ,$(this).next( a ).attr( href ));  
   }else{
      $( #prev ).attr( href ,$(this).prev( a ).attr( href ));
      $( #next ).attr( href , # ); 
   }
});




相关问题
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: &...

热门标签