Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向Iframe添加元素_Javascript_Html_Iframe - Fatal编程技术网

Javascript 向Iframe添加元素

Javascript 向Iframe添加元素,javascript,html,iframe,Javascript,Html,Iframe,我正在创建一个包含iframe的页面。我的目标是创建一个javascript,它将添加到iframe内容的头部 但我总是得到一个错误,暗示iframe.contentDocument为null 主页的代码 <html> <head> <title>Sample "Hello, World" Application</title> <script type="text/javascript" > function sizeFrame()

我正在创建一个包含iframe的页面。我的目标是创建一个javascript,它将添加到iframe内容的头部

但我总是得到一个错误,暗示iframe.contentDocument为null

主页的代码

<html>
<head>
<title>Sample "Hello, World" Application</title>
<script type="text/javascript" >
function sizeFrame() {
try{
    var ifrm = document.getElementById("CFrame");
    var bt = ifrm.contentDocument.createElement("base");
    bt.setAttribute("target", "_parent");
    ifrm.contentDocument.getElementsByTagName("head")[0].appendChild(bt);
}
catch (er)
{alert(er);}
}
</script>
</head>
<body>
<div id="sidecontainer">
<iframe id="SFrame" name="SearchFrame" frameborder ="0" width="500" height="100" scrolling="no" src="SearchBox.html"></iframe>
</div>
<div id="content">
<iframe id="CFrame" name="ContentFrame" frameborder="0" height="1000px" width="1000px" onload="sizeFrame()" scrolling="no" src="ContentFrame.html"></iframe>
</div>
</body>
</html>

示例“Hello,World”应用程序
函数sizeFrame(){
试一试{
var ifrm=document.getElementById(“CFrame”);
var bt=ifrm.contentDocument.createElement(“基”);
bt.setAttribute(“目标”,“父项”);
ifrm.contentDocument.getElementsByTagName(“头”)[0].appendChild(bt);
}
捕获(er)
{alert(er);}
}
Iframe ContentFrame.html的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
    <div id="input">
    test
    </div>
</body>
</html>

测试

使用框架窗口名称创建元素,就像在主窗口中一样,如下所示

ContentFrame.document...
换句话说,访问窗口名

此外,iframe窗口没有任何id值等于“id”的元素。

我建议您试试

ifrm.contentWindow.document.getElementById("id")
谢谢,另一个错误(TypeError:无法获取未定义的ElementById)。并且需求发生了变化,所以现在我需要创建一个新的“base”标记并将其添加到iframe中。