2011-08-13 84 views
4

我幾乎可以肯定我可以在這裏找到答案,但我沒有成功。 您將如何檢查在事件調用時是否有某個對象被選中(專注)?我試過if (document.activeElement == "[object HTMLBodyElement]"),但顯然.activeElement正在填寫上載。我不允許使用jQuery,只有mootools。檢查某些元素是否是沒有jQuery的「聚焦」

非常感謝您的幫助!

回答

3

https://developer.mozilla.org/en/DOM/document.activeElement - 如所指出的,如果沒有元件被聚焦,原稿體被默認聚焦:

if (document.activeElement != document.body) { 
    // something else than the body is focused 
} 
+0

謝謝,但'[對象HTMLBodyElement]'是 「activeElement」 默認情況下。我不明白的是,爲什麼當我將焦點改爲textarea時,它不會改變。 – mrkva

+0

如果'沒有'是重點然後document.activeElement是 – Matijs

+0

@Matijs哦,對。我更新了我的答案。謝謝。 – Shi

2
if (document.body === document.activeElement) { 
    // the body is focused, ie no other element is focused 
}