2013-06-11 32 views
0

我正在使用jQuery,並且正在繪製圖形。我在圖的右側和左側都有可滾動的箭頭。我所要求的功能是:在jquery中單擊按鈕時啓用按鈕

  • 每當數據更多,並且每當我們自動點擊右箭頭時,左側的可滾動箭頭必須可見。
  • 如果沒有更多數據,則右側按鈕必須是不可見的。
  • 如果沒有足夠的滾動數據,則左側按鈕必須是不可見的。
  • 總結:按鈕應該只可用,如果可以使用。

這裏是到目前爲止我的代碼:

$(function() { 
    var $box2 = $('.column'), totalWidth = 900, cursor = 0; 

    $("#rightarrrowbutton").click(function() { 
     if (cursor != totalWidth) { 
      $box2.animate({ 
       marginLeft : "-=15" 
      }, "fast"); 
      cursor += 100; 
     } 
    }); 
    if ($("#rightarrowbutton").click(){ 
     $('#leftarrowbutton').click(function() { 
      if (cursor != 0) { 
       $box2.animate({ 
        marginLeft : "+=15" 
       }, "fast"); 
       cursor -= 100; 
      } 
     }); 
    }); 
} 
+3

關閉主題:你的shift鍵壞了? – epascarello

+0

'if($(「#rightarrowbutton」)。click(function(){// ....'這總是正確的,順便說一句 –

回答

0

嘗試像

$("#rightarrrowbutton").click(function() { 
$("#leftarrowbutton").show(); 
}); 

    $("#leftarrowbutton").click(function() { 
$("#rightarrrowbutton").show(); 

    }); 
+0

你需要點擊右箭頭後檢查是否有更多東西要顯示如果沒有,則隱藏右箭頭,左邊相同 –

+0

謝謝Deepak。我在函數頂部添加了$(「#leftarrowbutton」)。hide();並且此$( 「#rightarrrowbutton」)。click(function(){(「#leftarrowbutton」)。show(); });在rightarrow按鈕函數之後,它工作完美 –

0

而不是試圖安裝和取下單擊處理程序,只需添加它們,當你的頁面加載和.hide().show()適當的按鈕。

$('#rightarrowbutton').click() { 
    // do your stuff 
    if (/*there's no more stuff to the right*/) { 
     $('#rightarrowbutton').hide(); 
    } 
    $('#leftarrowbutton').show(); 
} 

以及類似的左鍵。

+0

感謝您的回覆Matt。當我的右箭頭被自動按下時,我的左箭頭應該被啓用,如果不是,它必須被隱藏 –

+0

@HkM:Errm ...是的,這使左箭頭在點擊右箭頭時可見,所以我不明白你的投訴。 –

+0

我得到了解決方案馬特。非常感謝您的回覆 –

0

我知道你的描述是關於可見性的,但你的問題的標題提到了啓用。要禁用,可以將屬性"disabled"置於<button>元素中。如果有,它將被禁用,如果丟失它將被啓用。所以你可以在.click()回調期間刪除或添加屬性。

例如:

$("#leftarrowbutton").click(function() 
{ 
    // if meets ending criteria 
    if (true) 
    { 
     // enable other button 
     $("#rightarrowbutton").removeAttr("disabled"); 

     // disable current button 
     $("#leftarrowbutton").attr("disabled", true); 
    } 
});