我有以下带有两个<a>元素的HTML :
<!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>
Run Code Online (Sandbox Code Playgroud)
其中一个<a>元素在<head>元素中,另一个元素在元素中<body>.然而,在控制台上加载写入时上面的页面:
There's 2 links in the body, of which the first one is: [http://www.google.com/].
Run Code Online (Sandbox Code Playgroud)
更重要的是令人费解的,不断变化的document.body.getElementsByTagName,以document.documentElement.getElementsByTagName产生完全相同的结果.我是否理解document.body当时是别名document.documentElement并且实际上并未指向<body>HTML 的元素?
第一个锚不在头元素中.
通过尝试在头部开始锚点,您隐式地结束头元素并启动body元素.
然后,正文的head和start标记的结束标记是无效标记,将被忽略.