Inconsistent Whitespace Text Nodes in Internet Explorer

前端 未结 5 1630
一生所求
一生所求 2020-12-09 22:03

The following source code alerts the following results:

Internet Explorer 7: 29
Firefox 3.0.3: 37 (correct)
Safari

相关标签:
5条回答
  • 2020-12-09 22:17

    Zachleat, I think I've found a solution to the problem. (I've taken liberty of moving the form elements into the form tag.)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <!--REF http://stackoverflow.com/questions/281443/inconsistent-whitespace-text-nodes-in-internet-explorer -->
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function countNodes()
        { alert(document.getElementsByTagName('form')[0].childNodes.length);
        };
    </script>
    </head>
    <body onload="countNodes()">
      <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
      ></form>
    </body>
    </html>
    

    As you can see, I've split & strapped the closing gt (greater-than) brackets so they hug to the next tag, thus ensuring there are no ambiguous whitespaces.

    It was a nice surprise that it worked for all (4 desktop) browsers tried so far, all reporting the same number of DOM nodes.

    0 讨论(0)
  • 2020-12-09 22:24

    Well... I'd say the reason it that it is IE. I don't think the programers had a specific intention to do it that way.

    0 讨论(0)
  • 2020-12-09 22:25

    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 ("\n", <input> and "\n"), 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.

    0 讨论(0)
  • 2020-12-09 22:29

    I'm guessing that the table tag is different between browsers.

    e.g. which nodes does the default table auto-magically contain?

    <table>
      <thead>
      </thead>
      <tbody>
      </tbody>
      <tfoot>
      </tfoot>
    </table>
    
    0 讨论(0)
  • 2020-12-09 22:35

    Why not just try walking the DOM and see what each browser thinks that the document contains?

    IE does a lot of "optimization" of the DOM. To get an impression of what this might look like, "Select all", "Copy" in IE, and then "Paste Alternate" in Visual Studio, You get the following:

    <INPUT value="Submit Query" type=submit> 
    <INPUT value=Reset type=reset> 
    <INPUT type=button> 
    <INPUT type=text> 
    <INPUT value="" type=password> 
    <INPUT type=file> 
    <INPUT type=hidden>
    <INPUT type=checkbox>
    <INPUT type=radio>
    <BUTTON type=submit></BUTTON> 
    <SELECT></SELECT>
    <TEXTAREA></TEXTAREA> 
    

    So it nukes some of the empty tags and adds some default attributes.

    0 讨论(0)
提交回复
热议问题