Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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中包含的网页中创建span元素_Javascript_Iframe - Fatal编程技术网

Javascript 如何在iframe中包含的网页中创建span元素

Javascript 如何在iframe中包含的网页中创建span元素,javascript,iframe,Javascript,Iframe,我有一个场景,javascript函数在浏览器主窗口中的网页中创建一个新的span元素-- 下面给出了这方面的代码-- 现在我想扩展这段代码,使它也能在iframe中工作。。。我已经发现,为了实现这一点,应该修改上面的代码,以便在iframe中创建span元素。我有iframe的id/名称,其中必须创建span元素。。。如何在iframe中创建跨度元素 我知道iframe中的网页应该与主窗口中包含的网页属于同一个域(即同源策略).如果我理解正确,您需要在iframe中的网页中添加一个元素 使用此

我有一个场景,javascript函数在浏览器主窗口中的网页中创建一个新的span元素--

下面给出了这方面的代码--

现在我想扩展这段代码,使它也能在iframe中工作。。。我已经发现,为了实现这一点,应该修改上面的代码,以便在iframe中创建span元素。我有iframe的id/名称,其中必须创建span元素。。。如何在iframe中创建跨度元素


我知道iframe中的网页应该与主窗口中包含的网页属于同一个域(即同源策略).

如果我理解正确,您需要在iframe中的网页中添加一个元素

使用此代码

var ifrm= document.getElementById('iframe id');
var ifrmdocument = ifrm.contentWindow.document;
var spn=ifrmdocument.createElement('span');
spn.innerHTML='Hello';
ifrmdocument.appendChild(spn);
问候

我在这儿有什么问题吗


如果我的答案有用,请将其标记为正确答案

我刚刚在Firefox浏览器上测试了此代码,它在var ifrmdocument=ifrm.contentWindow.document上抛出异常;请告诉我如何在Firefox上修复此代码。谢谢。请告诉我你使用的FF版本??
var ifrm= document.getElementById('iframe id');
var ifrmdocument = ifrm.contentWindow.document;
var spn=ifrmdocument.createElement('span');
spn.innerHTML='Hello';
ifrmdocument.appendChild(spn);