【问题标题】:Why is $body.children() including elements that are not within the <body></body> tags?为什么 $body.children() 包含不在 <body></body> 标签内的元素?
【发布时间】: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,因为&lt;body&gt; 标签内只有三个元素。当我遍历子元素时,它显示 3 个元素是 &lt;p&gt; 元素(如预期的那样),而第四个是 &lt;script&gt; 元素。为什么 &lt;script&gt; 元素包含在 body 的子元素中,而它不在 HTML 中的 &lt;body&gt; 标签内?

【问题讨论】:

  • 因为您的 HTML 无效,典型的浏览器会尝试通过将脚本放置在 in 正文中来纠正它。

标签: html children


【解决方案1】:

只有&lt;head&gt;&lt;body&gt; 可以进入&lt;html&gt;。您的&lt;script&gt; 标签放置不正确,因此浏览器会将其错误更正为&lt;body&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 2019-10-26
    • 2016-10-30
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    相关资源
    最近更新 更多