上代码
var link = document.createElement("a");
link.innerText = "http://yyy.com";
link.href = "http://xxx.com";
document.body.innerHTML = link;
我知道正常情况下,给innerHTML赋的应该是一HTML字串,但这里讨论的是这种情况——赋的是一个DOM对象,body里的文字就是a的href值了,请解释下这个赋值的过程,谢谢。
ps: 本人尝试过link.toString(),输出的是href值(会不会跟这个有关?)。
是的
你可以通过重载link的toString()方法来确认
文档里写的很清楚。
On return, content contains the serialized HTML code describing all of the element's descendants.
Removes all of element's children, parses the content string(把内容解析成字符串toString()) and assigns the resulting nodes as children of the element.