The getelementbyid a label is why the link address isn't an object?

61 4

According to the official explanation, chooses the dom element, and then the following code, the selected div, the should be a string 'object ',? How to explain?

<!DOCTYPE HTML>
<html lang="en-US">
<head>
 <meta charset="UTF-8">
 <title></title>
 <script type="text/javascript">
 window.onload = function (){
 var demoLink = document.getElementById('demoLink');
 var demoSpan = document.getElementById('demoSpan');
 var demoDiv = document.getElementById('demoDiv');
 alert(demoLink);
 alert(demoDiv);
 alert(demoSpan);
 }
 </script>
</head>
<body>
 <a href="http://www.baidu.com" id="demoLink">这是一个链接</a>
 <span id="demoSpan">这是一个span标签</span>
 <div id="demoDiv">这是一个span标签</div>
</body>
</html>
3 Answers

126 1
<a href="http://ooxx.ooxx" id="foo">sample text</a>
<script type="text/javascript">
 var foo = document.getElementById('foo');
 console.log(foo);//打出来就知道了,foo没有问题
 alert(foo);//在这里,关键在于alert这个方法会执行一次toString,javascript中,对HTMLAnchorElement执行toString,得到的就是HTMLAnchorElement.href
 console.log(foo.toString());//返回 http://ooxx.ooxx 和alert(foo)一样
</script>
104 1
<a href="http://ooxx.ooxx" id="foo">sample text</a>
<script type="text/javascript">
 var foo = document.getElementById('foo');
 console.log(foo);//打出来就知道了,foo没有问题
 alert(foo);//在这里,关键在于alert这个方法会执行一次toString,javascript中,对HTMLAnchorElement执行toString,得到的就是HTMLAnchorElement.href
 console.log(foo.toString());//返回 http://ooxx.ooxx 和alert(foo)一样
</script>
...