我正在使用此代码来获取父对象-
alert(parent.document.getElementById('test:input'))
通过这样做,我得到了这个
[object HTMLTableElement]
但每当我尝试访问它的值时,它都会说未定义
。
parent.document.getElementById('test:input').value
并且设置该值也不起作用。
最佳答案
这是因为 table
元素没有 value
。它们具有子元素(可通过 children
[现代浏览器] 或 childNodes
属性访问 [childNodes
还将包括非 Element 子元素,例如文本节点) ,但没有值
。 value
用于 input
和 select
元素。
您可以使用the DOM访问和更新表的内容。例如,如果表存在并且有一个 tbody,这将在其中第一个 tbody
的末尾添加一行:
var table = parent.document.getElementById('test:input'),
tbody = table && table.querySelector("tbody");
if (tbody) {
tbody.insertAdjacentHTML("beforeend",
"<tr><td>Hi there</td></tr>"
);
}
如果您想完全替换其内容,您可以分配给innerHTML
:
parent.document.getElementById('test:input').innerHTML =
"<tbody><tr><td>Hi there</td></tr></tbody>";
关于JavaScript+iframe : How to set parent element HTMLTableElement from iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611102/