2011-07-28 35 views
2

我做了下面的腳本來重寫瀏覽器的默認概述系統:定義大綱問題

* 
{ 
    margin: 0; 
    outline-color: #C79700; 
    outline-width: 1px; 
    padding: 0; 
} 

$('document').delegate('[tabindex]', 'focusin focusout mousedown', function(event) 
{ 
    var target = $(event.target); 

    switch(event.type) 
    { 
     case 'focusin': 
     { 
      var clicked = target.data('clicked'); 

      target.removeData('clicked'); 

      if (clicked) 
       target.css('outline-style', 'none'); 
      else 
       target.css('outline-style', 'solid'); 

      break; 
     } 

     case 'focusout': 
      target.css('outline-style', 'none'); 
      break; 

     case 'mousedown': 
     { 
      if (target.prop('tagName') === 'B') 
       target = target.parent(); 

      if (!target.is(':focus')) 
       target.data('clicked', true); 
      else 
       target.css('outline-style', 'none'); 

      break; 
     } 
    } 
}) 

一切完美,但我有一個小問題:如果我激活另一個窗口(通過最小化瀏覽器或使用ALT + TAB)時,文檔失去焦點,元素也會失去......然後,如果我重新激活瀏覽器窗口,數據('clicked')顯然是錯誤的,如果選擇了[tabindex]元素,則由腳本。

我該如何預防?我嘗試了很多解決方案而沒有成功。有沒有類似的東西:

  if (clicked || browser-has-just-become-active) 
       target.css('outline-style', 'none'); 
      else 
       target.css('outline-style', 'solid'); 

很多很多,謝謝!

回答

2

我敢肯定,你可以使用:

window.onfocus 

或IE:

document.onfocusin