<!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.
這是因爲您無法自行關閉標籤''。 HTML無效,可能無法預測。 – JJJ 2012-03-12 15:40:34
在這裏解釋:http://stackoverflow.com/questions/97522/what-are-all-the-valid-self-closing-tags-in-xhtml-as-implemented-by-the-major-b – 2012-03-12 15:43:04
會不會在使用Content-Type「application/xhtml + xml」時工作? – 2012-03-12 15:50:25