2011-12-31 65 views
2

我有一個div,tabindex屬性設置爲'-1',因此它可以獲得鼠標點擊的焦點。對焦點DIV的點擊操作

<div tabindex='-1'></div> 

.div:focus{//some style} 

我想用jquery做什麼,當用戶點擊的div,但只有當它被選中(第二次點擊後)。

$('div').click(function(){ 
if($(this).is(':focus')){ 
//do something 
} 
}); 

但它不起作用,立即觸發行動。

編輯:包括代碼。

+0

請包括您與 – 2011-12-31 18:08:10

+0

您的代碼[作品試圖代碼對我來說](http://jsfiddle.net/davidThomas/VygWj/),在Chromium 14/Ubuntu 11.04中...... – 2011-12-31 18:24:42

+0

大衛,這裏的問題是警報顯示,只要你點擊div在同一時間它越來越關注,而不是之後。 – olanod 2011-12-31 18:57:42

回答

2

我會檢查的第2點擊一個標誌,看到我answer這裏

  1. 我用一個標誌isClicked來確定它是第一次點擊還是第二次。如果是第一次點擊,則將該標誌設置爲true並返回(即不做任何事)。下一次,該標誌被檢查爲真,這意味着它是第二次點擊,並添加下面的代碼來做你想做的。

    var isClicked = false; 
    
    $('div#myTest').click(function(){ 
    
    //do nothing on 1st click 
    if (isClicked == false) { 
        alert('Clicked 1st time');  
        isClicked = true; 
        return; 
    } 
    
    //do whatever u want on second click  
    alert('Clicked 2nd time');  
    
    }); 
    
  2. 復位標誌被點擊的股利之外時,下面的代碼,這是否......

    // to check it is clicked outside div 
    $(document).click(function(e){ 
        if($(e.target).is('#myTest'))return;  
    
        //reset to 
        isClicked = false; 
    
    }); 
    
2

從我讀過的內容中,也許你可以在關注DIV後調用「click」動作。第一次點擊重點它,第二次點擊觸發下一個塊。

**編輯**研究後,您不能在DIV上使用焦點處理程序。我在想什麼!

*$('.double-click').focus(function(){ 
    alert("I'm now in focus"); 
    $(this).click(function(){ 
      alert("I'm now double clicked."); 
    }); 
});* 

**這是未經過測試的代碼。

我也看了看周圍jQuery的API,並注意到DBLCLICK()函數,你可以在這裏看到... http://api.jquery.com/dblclick/

+0

劃痕。事實證明,你不能「專注」DIV的。嘗試使用dblclick()函數。 – wwwroth 2011-12-31 18:25:45