Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

html - Inconsistent Whitespace Text Nodes in Internet Explorer

The following source code alerts the following results:

Internet Explorer 7: 29
Firefox 3.0.3: 37 (correct)
Safari 3.0.4 (523.12.9): 38
Google Chrome 0.3.154.9: 38

Please ignore the following facts:

  • Webkit (Safari/Chrome) browsers insert an extra text node at the end of the body tag
  • Internet Explorer doesn't have new lines in their whitespace nodes, like they should.
  • Internet Explorer has no beginning whitespace node (there is obvious whitespace before the <form> tag, but no text node to match)

Of the tags in the test page, the following tags have no whitespace text nodes inserted in the DOM after them: form, input[@radio], div, span, table, ul, a.

My question is: What is it about these nodes that makes them the exception in Internet Explorer? Why is whitespace not inserted after these nodes, and is inserted in the others?

This behavior is the same if you switch the tag order, switch the doctype to XHTML (while still maintaining standards mode).

Here's a link that gives a little background information, but no ideal solution. There might not be a solution to this problem, I'm just curious about the behavior.

Thanks Internet, Zach

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
        function countNodes()
        {
            alert(document.getElementsByTagName('body')[0].childNodes.length);
        }
        </script>
    </head>
    <body onload="countNodes()">
        <form></form>
        <input type="submit"/>
        <input type="reset"/>
        <input type="button"/>
        <input type="text"/>
        <input type="password"/>
        <input type="file"/>
        <input type="hidden"/>
        <input type="checkbox"/>
        <input type="radio"/>
        <button></button>
        <select></select>
        <textarea></textarea>
        <div></div>
        <span></span>
        <table></table>
        <ul></ul>
        <a></a>
    </body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IE tries to be helpful and hides text nodes that contain only whitespace.

In the following:

<p>
<input>
</p>

W3C DOM spec says that <p> has 3 child nodes (" ", <input> and " "), IE will pretend there's only one.

The solution is to skip text nodes in all browsers:

var node = element.firstChild;
while(node && node.nodeType == 3) node = node.nextSibling;

Popular JS frameworks have functions for such things.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...