2009年1月22日 星期四

How to get the focused element of the web page?

Reference from WebDeveloper.com
How can get the id of the focused element of the web page?

The following codes were being tested on my Firefox 3 and Google Chrome.
Thanks Declan1991.

Code:
function getTarget(e) {
e = e || window.event;
return e.target || e.srcElement;
}
document.onmouseup = function(e) {
var t = getTarget(e);
if ((t.type && t.type == "text") || t.nodeName == "textarea") {
//is text area of input[type=text]
}
}


Or, to get the focused element in form, you can reference the folling.
Both of them need the element in a form to attach functions to the focus event.
Get the element that has focus
Using javascript to check the focus of an element

The way to attach events, please read
非常好的javascript:add event/ remove event
javascript attachEvent 事件
attachEvent() / addEventListener()

沒有留言:

Powered By Blogger