【发布时间】:2016-03-31 04:23:34
【问题描述】:
这是我的 HTML:
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
</head>
<body>
<p>one</p>
<p>two</p>
<p>three</p>
</body>
<script>
var $body = $('body');
var children = $body.children();
alert(children.length) //-> 4
</html>
我希望alert(children.length) 提醒3 而不是4,因为<body> 标签内只有三个元素。当我遍历子元素时,它显示 3 个元素是 <p> 元素(如预期的那样),而第四个是 <script> 元素。为什么 <script> 元素包含在 body 的子元素中,而它不在 HTML 中的 <body> 标签内?
【问题讨论】:
-
因为您的 HTML 无效,典型的浏览器会尝试通过将脚本放置在 in 正文中来纠正它。