我正在升級腳本以使其與瀏覽器交叉。我現在的代碼如下。Javascript,檢測文本框是否有焦點?
function testFocus(){
var testSelection = document.getElementById('chattext').contentWindow.
window.document.selection.createRange().parentElement();
while (testSelection)
{
if (testSelection.id == "chatContent") {
return true;
}
testSelection = testSelection.parentElement;
}
return false;
}
但是,以下代碼不再適用於現代瀏覽器。目前上面的代碼必須選擇文本。它只需要檢查文本框是否有焦點。在按鈕/ javascript添加文本之前,該功能用作檢查。
除了_Alex C_的發佈,您還可以閱讀MDN中的document.activeElement:https://developer.mozilla.org/en/DOM/document。 activeElement和MSDN:http://msdn.microsoft.com/en-us/library/ms533065%28v=vs.85%29.aspx –