我创建了新的 html 元素。并初始化其值。然后,当我尝试通过 el.title
获取其标题时,它返回 undefined
。不过我可以通过 getElementsByTagName("title")
示例如下;
var el = document.createElement( 'html' );
el.innerHTML = "<!DOCTYPE html><html><head><title>HTML Reference</title></head><body>The content of the document......</body></html>";
console.log(el.title)
**undefined**
//but the following returns the title
el.getElementsByTagName("title")[0].text
"HTML Reference"
为什么会发生这种情况?为什么el.title
返回undefined
?
最佳答案
title
HTML 元素对象的属性映射到 the title
attribute ,它找不到第一个标题元素后代。
如果您有<html title='bad place for a title'>
,您将得到结果
关于javascript - 为什么 ".title"无法到达动态创建的 html 元素的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741262/