[英]Why does document.write and console.log give different outputs for getElementById?
var myListItems = document.getElementById ("li");
function myList () {console.log (myListItems)};
function myWrist () {document.write (myListItems)};
myList();
myWrist();
For the JavaScript above, why does document.write output - [object HTMLLIElement]
, whereas console.log outputs <li id="li">fdsf</li>
? 对于上述JavaScript,为什么document.write输出- [object HTMLLIElement]
,而console.log输出<li id="li">fdsf</li>
?
Any help would be appreciated.. 任何帮助,将不胜感激..
The console varies depending on browser. 控制台因浏览器而异。 However, most consoles will output the object and its structure if one exists whereas when using document.write it calls toString
and then writes the result of that to the page. 但是,大多数控制台将输出对象及其结构(如果存在),而使用document.write时,它将调用toString
,然后将结果写入页面。
That is why you see the object representation in the console, versus the string representation on the page. 这就是为什么您在控制台中看到对象表示形式而在页面上看到字符串表示形式的原因。
console.log(document.querySelector("li").toString());
<li></li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.