我有以下页面布局
<div id="userscore" >test</div>
<iframe name="frameContent" id="frameContent" width="100%" height="100%" frameborder="0" />
iFrame 动态获取其内容,包括其 javascript。
当我尝试从 iFrame 中访问 userscore 时
console.log(window.parent.document.getElementById('user-score'));
我进入控制台
<div id="userscore" >test</div>
当我尝试使用
window.parent.document.getElementById('user-score').html('Change the content');
我明白了
Object [object HTMLDivElement] has no method 'html'
有解决办法吗?或者任何人都可以阐明正在发生的事情。
最佳答案
DOM 元素没有方法 .html()
。该方法是 jQuery 等 JavaScript 库中常见的扩展。
使用纯 JavaScript 时,如果您想更改元素的内容,则必须设置 .innerHTML
属性。
window.parent.document.getElementById('user-score').innerHTML = "New content";
关于javascript - 从 iFrame 中更改 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318338/