Check whether it is a new HTML5 tag

Source: Internet
Author: User

HTML5 adds many new semantic and functional tags. Although we can select them through document. getelementbyid and document. getelementsbytagname, a problem occurs when copying nodes. The innerhtml that should have appeared is a null string ......

 
<! Doctype HTML>  

Therefore, we need to treat them differently when copying them, which is a prerequisite for how to identify them.

The following is a small test, which selects a scheme based on the log differences of different browsers:

Window. onload = function () {var test = document. createelement ("nav"); console. log (test. outerhtml); console. log (test. tagname); console. log (object. prototype. tostring. call (TEST ));}

The result is as follows:

 ie9 log:

  

   
   log: nav log: IE8 mode log of [object htmlunknownelement] ie9:

  

   
   log: nav log: IE7 mode log of [object] ie9:

  

   
   log: nav log: [object] IE8 log:

  

   
   log: nav log: [object] IE7 log:

  

   
   log: nav log: [object] ff12

   
   nav [object htmlelement] chrome18

   
   nav [object htmlelement] opera11

   
   nav [object htmlelement] safari5

   
   nav [object htmlelement] 

Interestingly, if you have created a new tag that is not HTML5, The tostring of the element for opera, chrome, and FF are all [object htmlunknownelement]. But here we only talk about ie. IE is obviously regarded as inserting an XML element. Therefore, we only determine whether outerhtml (ff11 only supports outerhtml), and whether outerhtml uses"

Below is my framework replication NodeCode:

VaR DIV = document. createelement ("Div"); // cache parser to prevent repeated creation of function shimclonenode (outerhtml, tree) {tree. appendchild (DIV); Div. innerhtml = outerhtml; tree. removechild (DIV); Return Div. firstchild;} var unknowntag = "<? XML: namespace "function clonenode (node, dataandevents, deepdataandevents) {var bool //! Undefined = true; // This determination must be so long: Determine whether a new label can be cloned, determine whether it is an element node, and determine whether it is a new label if (! Support. clonehtml5 & node. outerhtml) {// delayed creation detection element var outerhtml = document. createelement (node. nodename ). outerhtml; bool = outerhtml. indexof (unknowntag )//! 0 = true;} var NEO =! Bool? Shimclonenode (node. outerhtml, document.doc umentelement): node. clonenode (true), SRC, Neos, I; // support. clonehtml5: Doc. createelement ("nav "). clonenode (true ). outerhtml! = "<: Nav> </: nav> ",//...... }

If you are interested, you can go to my GitHub for a transfer.

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.