Currently, the document. activeelement attribute is supported, but some situations occur in Safari and chrome ...... Check stackoverflow, but the method is not reliable, because the click event always occurs before the focus event.
// Http://stackoverflow.com/questions/483741/how-to-determine-which-html-page-element-has-focusfunction _ dom_trackactiveelement (EVT) {If (EVT & evt.tar get) {document. activeelement = evt.tar get = document? Null: evt.tar get;} function _ dom_trackactiveelementlost (EVT) {document. activeelement = NULL;} If (! Document. activeelement) {document. addeventlistener ("Focus", _ dom_trackactiveelement, true); document. addeventlistener ("Blur", _ dom_trackactiveelementlost, true );}
The standard browser supports domactivate, but it is also too slow to help.
<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> activeelement by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> </P> <p> window. onload = function () {<br/> var safari =//. _ PROTO __= = '//'; <br/> // http://www.quirksmode.org/js/detect.html <br/> var chrome = navigator. useragent. indexof ("Chrome ")! =-1; </P> <p> var addevent = (function () {<br/> If (document. addeventlistener) {<br/> return function (El, type, FN) {<br/> el. addeventlistener (type, FN, false); <br/>}; <br/>}else {<br/> return function (El, type, FN) {<br/> el. attachevent ('on' + type, function () {<br/> return fn. call (El, window. event); <br/>}); <br/>}< br/> })(); </P> <p> function getactiveelement (e) {<br/> var M = document. activeelement; </P> <p> alert (m) <br/>}</P> <p> var inputs = document. getelementsbytagname ("input"); <br/> for (VAR I = 0, n = inputs. length; I <n; I ++) {<br/> addevent (inputs [I], "click", getactiveelement) <br/>}</P> <p> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <form action = "#"> <br/> <input type = "checkbox" name = "team" value = "team" Checked> spurs <br> <br/> <input type = "checkbox" name = "team" value = "Pacers"> Pacers <br> <br/> <input type = "checkbox" name = "team" value = "Kings"> kings <br> <br/> <input type = "checkbox" name = "team" value = "other"> Other <br/> <p> <br/> <input type = "button" id = "AA" value = "1"> <br/> </P> <br/> <p> <br/> <input type = "button" id = "BB" value =" 2"> <br/> </P> <br/> <p> <br/> <input type = "button" id = "cc" value = "3 3"> <br/> </P> <br/> </form> <br /> </body> <br/> </ptml> <br/>
RunCode
Note: My goal is to get the current activation element, not to get the element of the current click ......
<Br/> <! Doctype HTML> <br/> <HTML dir = "LTR" lang = "ZH-CN"> <br/> <pead> <br/> <meta charset = "UTF-8" /> <br/> <title> activeelement by situ zhengmei </title> <br/> <SCRIPT type = "text/JavaScript"> </P> <p> window. onload = function () {</P> <p> var safari =//. _ PROTO __= = '//'; <br/> // http://www.quirksmode.org/js/detect.html <br/> var chrome = navigator. useragent. indexof ("Chrome ")! =-1; </P> <p> var addevent = (function () {<br/> If (document. addeventlistener) {<br/> return function (El, type, FN) {<br/> el. addeventlistener (type, FN, false); <br/>}; <br/>}else {<br/> return function (El, type, FN) {<br/> el. attachevent ('on' + type, function () {<br/> return fn. call (El, window. event); <br/>}); <br/>}< br/> })(); </P> <p> function getactiveelement (e) {<br/> va R m; <br/> If (Safari | chrome) {<br/> M = e.tar get = document? Null: e.tar get; <br/>}else {<br/> M = document. activeelement; <br/>}< br/> alert (m) <br/>}</P> <p> var inputs = document. getelementsbytagname ("input"); <br/> for (VAR I = 0, n = inputs. length; I <n; I ++) {<br/> addevent (inputs [I], "click", getactiveelement) <br/>}</P> <p> </SCRIPT> </P> <p> </pead> <br/> <body> <br/> <form action = "#"> <br/> <input type = "checkbox" name = "team" value = "team" Checked> spurs <br> <br/> <input type = "checkbox" name = "team" value = "Pacers"> Pacers <br> <br/> <input type = "checkbox" name = "team" value = "Kings"> kings <br> <br/> <input type = "checkbox" name = "team" value = "other"> Other <br/> <p> <br/> <input type = "button" id = "AA" value = "1"> <br/> </P> <br/> <p> <br/> <input type = "button" id = "BB" value =" 2"> <br/> </P> <br/> <p> <br/> <input type = "button" id = "cc" value = "3 3"> <br/> </P> <br/> </form> <br /> </body> <br/> </ptml> <br/>
Run code
I remember that I had seen this mechanism in the back-lighting blog before, but this blog was destroyed in the flood of crabs ......