从 iFrame 内部将 HTML 附加到父级,而无需 jQuery
Appending HTML to parent from inside iFrame without jQuery
我正在开发一个应用程序,其中新代码仅通过iframe引入。 我正在尝试创建一个函数(在 iFrame 中运行),该函数将在运行它的 iFrame 之后附加一些 html。 我宁愿不引入jQuery来解决这个问题。 这是一个示例
function AppendDivAfteriFrame() {
var iFrame = window.parent.document.getElementById(window.frameElement.id)
var newDivInParent = window.parent.document.createElement('div');
newDivInParent.setAttribute("Id", "MyDiv");
newDivInParent.innerHTML = 'Hello World! I am outside the iFrame';
iFrame.appendChild(newDivInParent);
}
我没有收到任何异常,但我也从未看到任何结果。
使用完整代码更新
将此页面称为内部页面.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function AppendDivAfteriFrame() {
var iFrame = window.parent.document.getElementById(window.frameElement.id)
var newDivInParent = window.parent.document.createElement('div');
newDivInParent.setAttribute("Id", "MyDiv");
iFrame.appendChild(newDivInParent);
parent.document.getElementById('MyDiv').innerHTML = 'Hello World! I am outside the iFrame';
}
</script>
</head>
<body>
<button onclick="AppendDivAfteriFrame()">Append HTML</button>
</body>
</html>
将此页面称为外页.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<iframe src="InnerPage.html" id="MyiFrame"></iframe>
</body>
</html>
这有效:
function AppendDivAfteriFrame() {
var iFrame = window.parent.document.getElementById(window.frameElement.id)
var newDivInParent = window.parent.document.createElement('div');
newDivInParent.setAttribute("Id", "MyDiv");
iFrame.parentNode.appendChild(newDivInParent); //Edited here
parent.document.getElementById('MyDiv').innerHTML = 'Hello World! I am outside the iFrame';
}
相关文章:
- 角度ng-repeat不更新,当使用jQuery内部指令调用Angular 方法时
- 如果它在 jQuery 内部,则重复将不起作用
- 如何访问jquery内部数据
- jquery内部的注释更多的是jquery
- 如何将变量传递给JQuery内部的函数
- Jquery内部引号添加了一个变量
- 调用jquery内部定义的javascript方法
- 从jquery内部的另一个javascript调用函数
- jQuery内部服务器错误
- jquery内部用户控件动态创建
- jquery内部的Javascript抛出语句准备好了
- 访问javascript native "this"jQuery内部
- jQuery内部的jQuery函数都没有按预期工作
- 使用jquery内部或外部文档准备
- 谷歌饼图使用Jquery内部的一个函数
- jQuery内部html主体
- jQuery内部函数不工作
- Jquery内部ajax (get)不能在phonegap + android 2.2上工作
- PHP Ajax加载文件与jQuery内部文件
- jquery内部OOP函数的问题