Clonenode compatibility issues

Source: Internet
Author: User

I think this method is not a private Implementation of IE. It can achieve perfect copy in IE, And the other seems to be a little strong, it is similar to the replacenode naming method.

In IE, clonenode can copy all attributes, attributes, and expando attributes of a node, and all events ), the standard browser can only copy the first two. However, unfortunately, sometimes the standard browser cannot tell which one is, and once it is regarded as a custom attribute, it cannot be restored.

      var div = document.createElement("div");      div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>";      var clone = div.firstChild.cloneNode(true);      alert(clone.checked)

The above code returns false by mistake in safari4.

Note: If the clonenode parameter is true, the child will be copied, if they are nodes. However, this parameter is required in Firefox and opera. If it is set to true or false, the parameter cannot be omitted. Otherwise, an exception is thrown, indicating that the parameter is insufficient. For security, this Boolean must be written.

But IE does not have any bugs. Let's look at the experiment below:

<! Doctype HTML> 

<Br/> <! Doctype HTML> <br/> <pead> <br/> <meta charset = "UTF-8"/> <br/> <meta content = "ie = 8 "http-equiv =" X-UA-compatible "/> <br/> <meta name =" keywords "content =" ie replication node bug by situ zhengmei "/> <br/> <meta name = "Description" content = "ie replication node bug by situ zhengmei"/> <br/> <title> ie replication node bug </title> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var node = document. getelementbyid ("AAA"); <br/> node. expando = {key: 1} <br/> var clone = node. clonenode (false); <br/> node. expando. key = 2 // corrected to 2 <br/> alert (clone. expando. key) // the attributes of the clone body are also corrected, symptoms similar to superficial replication <br/>}</P> <p> </SCRIPT> <br/> </pead> <br/> <body> <br/> <Div id = "AAA"> target node </div> </P> <p> </body> <br/> </ptml> <br/>

Run code

What if we fix the cloned object?

Window. onload = function () {var node = document. getelementbyid ("AAA"); node. expando = {key: 1} var clone = node. clonenode (false); clone. expando. key = 2 // modify the clone body alert (node. expando. key) // view the original object, 2}

<Br/> <! Doctype HTML> <br/> <pead> <br/> <meta charset = "UTF-8"/> <br/> <meta content = "ie = 8 "http-equiv =" X-UA-compatible "/> <br/> <meta name =" keywords "content =" ie replication node bug by situ zhengmei "/> <br/> <meta name = "Description" content = "ie replication node bug by situ zhengmei"/> <br/> <title> ie replication node bug </title> </P> <p> <SCRIPT type = "text/JavaScript"> <br/> window. onload = function () {<br/> var node = document. getelementbyid ("AAA"); <br/> node. expando = {key: 1} <br/> var clone = node. clonenode (false); <br/> clone. expando. key = 2 // modify the clone <br/> alert (node. expando. key) // view the original object, 2 <br/>}</P> <p> </SCRIPT> <br/> </pead> <br/> <body> <br/> <Div id = "AAA"> target node </div> </P> <p> </body> <br/> </ptml> <br/>

Run code

However, this experiment is not feasible in a standard browser because it does not support copying Custom Attributes. However, it also tells us that it is not appropriate to store data on element nodes. We should build a cache system like jquery.

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.