2012-10-25 223 views
0

我想使用.focus()將我的滑塊前進到下一個屏幕。我有下一個和後退按鈕。如果用戶點擊下一個按鈕,我不希望 .focus()函數提前滑塊。但是,如果焦點位於下一個按鈕上,我希望焦點觸發下一個按鈕.click()函數。有沒有辦法檢查鏈接是否被點擊?

這就是我現在的樣子。當我選中並將焦點放在下一個按鈕上時,滑塊前進。當我點擊下一個按鈕時,滑塊前進兩次。

$(".nextbutton4").focus(function() { 
    $(".nextbutton4").click(); 
}); 

我想出了帶有if-else語句的另一個版本的focus()函數,但它不起作用。

// This allows a tab advancement to trigger the next action 
$(".nextbutton4").focus(function() { 
    if (//This is where I would like to check whether the button was clicked) { 
      alert('Next Button is Clicked'); 
      // Do Nothing 
    } 
    else { 
      alert('Next Button Not Clicked'); 
      $(".nextbutton4").click(); 
    } 
}); 

回答

1

你可以嘗試添加類,當用戶點擊該鏈接:

$('.nextbutton4').click(function() { $(this).addClass('clicked'); }); 

,然後簡單地檢查,看其是否具有類:

// This allows a tab advancement to trigger the next action 
$(".nextbutton4").focus(function() 
{ 
    if ($(this).hasClass('clicked')) 
    { 
      alert('Next Button is Clicked'); 
      // Do Nothing 
    } 
    else 
    { 
      alert('Next Button Not Clicked'); 
      $(this).click(); 
    } 
}); 
+0

這是一個很好的解決方案但是它不會阻止點擊過程中的進度重複。 –

+0

當用戶單擊下一個按鈕時,焦點將應用於該按鈕。然後觸發點擊功能和焦點功能。 –

+0

攔截click函數並通過邏輯調用nextSlide函數會更好,而不是觸發實際的「click」事件... – BenM

相關問題