InnerHTML is a special case of execution in MozillaFirefox and Opera. _ Javascript skills

Source: Internet
Author: User
InnerHTML is a special case of execution in MozillaFirefox and Opera. This is a problem I saw in the CSDN forum. I usually did not pay attention to it, or I did not use it like this. View code

<Body id = "www.never-online.net"> script var foo = function () {var $ = function () {return document. getElementById (arguments [0]);} $("d2 "). innerHTML = $("d1 "). innerHTML;} //]> script changes the values in the two text boxes and assigns innerHTML of the container to d2 <button onclick = "foo ()"> foo () </button> <p id = "d1" style = "color: red"> <input value = 'blog. never-online '/> <input value = ""/> </p> <p id = "d2"> </p> </body>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]



Not the expected result, right? The execution in Opera is the same as that in Firefox. You can try to run it in Opera.

What if we can achieve the expected results? I tried to use the _ defineGetter _ method to get all innerHTML in Mozilla, but here I am given some tips. For example:

<Script type = "text/javascript"> function elementDetail (e) {if (! E. attributes) return ""; var attrs = e. attributes; var str = "<" + e. tagName; for (var I = 0; I "; return str;} function foo () {var a = document. getElementById ("d1"); var str = ""; for (var I = 0; I change the values in the two text boxes, assign innerHTML of this container to d2 <button onclick = "foo ()"> foo () </button> <p id = "d1" style = "color: red "> <input value = 'blog. never-online '/> <input value = "script by never-online"/> </p> <p id = "d2"> </p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Although the above Code fails to achieve the expected purpose, it is necessary to know the value of attribute, so I try to use the following method to do it.

<Script type = "text/javascript"> var $ = function (id) {return document. getElementById (id);} function foo () {$ ("d2 "). innerHTML = $("d1 "). innerHTML;} in script, change the values in the two text boxes and assign the innerHTML of the container to d2 <button onclick = "foo ()"> foo () </button> <p id = "d1"> <input value = 'blog. never-online 'onkeyup = "this. setAttribute ('value', this. value) "/> <input value =" script by never-online "/> </p> <p id =" d2 "> </p>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


If this method succeeds, it means that innerHTML under Mozilla gets its innerHTML by traversing the tagName of all nodes of the element and attrubutes. Therefore, the innerHTML value in the first example cannot be changed.

At the same time, Opera can also use this trick. It is also proved that the innerHTML in Opera is also obtained by traversing the tagName of all nodes of the element and attrubutes.

Is the w3c standard like this? There is no time to query w3c information, so it is not a coincidence between Opera and Mozilla, or a more comfortable design for developers in IE.

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.