我有以下HTML,其中包含两个<a>
元素:
<!doctype html>
<html>
<head>
<a href="http://www.google.com">www.google.com</a>
<script>
function onLoad() {
var bodyLinks = document.body.getElementsByTagName("a");
var msg = "There's "+bodyLinks.length
+" links in the body, of which the first one is: ["+bodyLinks[0].href+"].";
console.log(msg);
}
window.onload = onLoad;
</script>
</head>
<body>
<br/>
<a href="http://www.stackoverflow.com">www.stackoverflow.com</a>
</body>
</html>
其中一个<a>
元素在<head>
元素中,另一个在<body>
中。然而,上面的页面加载后写在控制台上:
There's 2 links in the body, of which the first one is: [http://www.google.com/].
更令人费解的是,将document.body.getElementsByTagName
更改为document.documentElement.getElementsByTagName
会产生完全相同的结果。我是否正确理解document.body
是document.documentElement
的别名,实际上并没有指向HTML的<body>
元素?
第一个锚点不在head元素中。
- 头部构件内不允许使用锚固件
- 头元素的结束标记是可选的
- body元素的起始标记是可选的
通过尝试在head中启动锚点,可以隐式地结束head元素并启动body元素。
head的结束标记和body的开始标记则是无效标记,并被忽略。