2012-03-12 13 views
0
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Test</title> 
    </head> 
    <script type="text/javascript"> 
     function fire() { 
      console.log(document.body.children.length); 
      console.log(document.body.children[0].children.length); 
     } 
    </script> 
    <body onload="fire()"> 
     <div> 
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pharetra ipsum erat. Aenean convallis laoreet massa, et gravida purus faucibus ac. <a id="test"/>Vestibulum libero justo.</p> 
     </div> 
    </body> 
</html> 

When you open the console, you'll see that the <a> is a children of <body>, <div> and <p>. WTF??なぜですか<a id="test"/> breaks out of the DOM chain?

If <a> is closed with a "proper" closing tag the issue disappears and its only child of <p>.

Can someone explain this to me?

PS: Tested on Chrome stable.

+1

ここで説明します:http://stackoverflow.com/questions/97522/what-are-all-the-valid-self-closing-tags-in-xhtml-as-implemented-by-the-major-b –

+0

Content-Type "application/xhtml + xml"を使用して作業していますか? –

答えて

8

I imagine it's because a is not a valid short tag.

+0

Content-Type "application/xhtml + xml"を使用しても動作しますか? –

+0

それでも、ブラウザが使用するXHTML/HTML5は有効ではありません。あなたは* XMLとしてそれを解析することができますが、ブラウザはこれをしません。 – Matthew

3

As per the W3C recommdentations:

12.2 The A element [...] Start tag: required, End tag: required

It is invalid HTML. You need to at least close the tag directly after such as <a id="test"></a>.

Source: http://www.w3.org/TR/html401/struct/links.html

1

<a> tagsは終了タグ(</a>)が必要です。

<a />は無効であり、ブラウザは何を意味するのか把握しようとしますが、一貫した動作は期待しません。

1

w3schools.com/tags/default.aspから、あなたは自己終了タグを見ることができます:

"area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "link", "meta", "param" 

Aタグは自己閉じてはなりません。

+0

W3Schoolsですか? [W3Fools!](http://w3fools.com) – Gareth

+0

悪であることは分かりませんでしたが、タグの終わりのような簡単なことについては、まったく正しいはずはありませんか? –

1

これはxmlではなくhtml5です。 void elementsのみがそのような表現を持つことが許される。 8.1.2.1を参照してください。6

関連する問題