Does activeelement always return the body in Safari and chrome?

Source: Internet
Author: User

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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.