Queryselector usage Improvement

Source: Internet
Author: User

the selector only supports one context and avoids IE8 bugs.

If (! Flag_xml & Doc. queryselectorall) {// ff, opera, chrome, and Safari XML files also implement the queryselectorall interface, but cannot use VaR query = expr; // getelementsbytagname of IE, queryselectorall has a problem in searching the child of the object element. If (doc.doc umentmode ===8 & context. nodetype = 1 & context. nodename. tolowercase ()! = "Object") {var id = context. getattribute ("ID"), uid = context. uniqueid // when the context is an element section, the search result contains your own if (! ID) {context. setattribute ("ID", UID);} Try {return pushresult (context. queryselectorall ("#" + uid + "" + query), result, flag_multi);} catch (e) {} finally {If (ID = context. uniqueid) {context. removeattribute ("ID") ;}} else {If (/\! \=/. Test (query) // manually supports e [ATTR! = Val] query = query. Replace (/\ [\ s * (\ W +) \ s *! = \ S * (['"] *). *? \ 3) \ s * \]/g, ": Not ([$1 = $2])"); try {return pushresult (context. queryselectorall (query), result, flag_multi);} catch (e ){}}}

The improvement is as follows. Now we only need to avoid the object bug of IE.

If (! Flag_xml & Doc. queryselectorall) {// http://www.w3.org/TR/selectors/ node = context; // use a replacement so that the multi-context implementation does not rearrange the search var usecontains = false; If (contexts. length> 2 | doc.doc umentmode ===8 & context. nodetype = 1) {node = Doc; usecontains = true;} if(doc.doc umentmode! = 8 | node. nodename. tolowercase ()! = "Object") {try {nodes = pushresult (node. queryselectorall (expr), result, flag_multi); var ret = [] If (usecontains & nodes. length) {var Cn = contexts. length; for (I = 0, rI = 0; I

Another improvement is to add a class name to the context of the element node so that the search can be completed in one step. The disadvantage is that you must operate the class before and after.

If (! Flag_xml & Doc. queryselectorall) {var query = expr; If (contexts. length> 2 | doc.doc umentmode = 8 & context. nodetype = 1) {If (contexts. length> 2) Context = Doc; query = ". fix_icarus_sqa "+ query; // use the class name to make sure that the search range is for (I = 0; node = contexts [I ++];) {If (node. nodetype = 1) {node. classname = "fix_icarus_sqa" + node. classname ;}}if(doc.doc umentmode! = 8 | context. nodename. tolowercase ()! = "Object") {try {result = pushresult (context. queryselectorall (query), result, flag_multi); If (query. indexof (". fix_icarus_sqa ") = 0) {// If a class name is added to the context, go to the class name for (I = 0; node = contexts [I ++];) {If (node. nodetype = 1) {node. classname = node. classname. replace ("fix_icarus_sqa", "") ;}} return result;} catch (e ){}}}

However, it is much faster to call Dom. Contains by performing operations on classes.

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.