【问题标题】:Insert a value in iFrame在 iFrame 中插入一个值
【发布时间】:2013-09-19 15:28:36
【问题描述】:
我有这个结构,我想根据之前的表单更改表单的值。问题是,iframe 没有 ID,我无法设置 ID,因为它是一个从另一个域检索数据的插件。
...<input type="button" onclick="document.getElementById('betterplace').style.display='block';
var betterplace = document.getElementById('betterplace')[0];betterplace.getElementsByTagName('iframe')[0].document.getElementById('donation_presenter_address_first_name').value=document.getElementById('_vorname').value;
" style="border:none; height: 50px; width: 250px; font-weight:bold; font-size: 14pt; background-color:#94C119;color:white;cursor: pointer;" value="Ihre Spende abschließen"/>
<div id="betterplace" style="display:none;">
<div style="width:500px;height:2px;margin-bottom:5px;background-color: #94c119"/>
<br/>
<script type="text/javascript">
...
【问题讨论】:
标签:
javascript
html
wordpress
forms
iframe
【解决方案1】:
这样的东西会调用框架:
parent.frames[1].doWhatEver
所有框架都放入一个数组中,您网站的第一个框架将是parent.frames[0],第二个框架将是parent.frames[1],依此类推。
编辑:
如果这不起作用,您可以随时使用此方法:
parent.getElementsByTagName("iframe")[0].doWhatEver
// or
parent.getElementsByTagName("frame")[0].doWhatEver
getElementsByTagName(tagName) 返回一个数组,其中的元素具有tagName(例如<iframe>)。有了这个,你可以得到你的框架。
【解决方案2】:
我无法理解您的问题,但如果您想在 iframe 中插入一些文本或 html,您可以这样做:
var iframe = document.getElementById('your iframe id') // if the iframe is already exists
doc, div;
doc = iframe.contentDocument || iframe.contentWindow.document;
doc.open();
doc.write('some text') // if you want to write to the iframe
doc.close();
div = doc.createElement('div');
// this div will appear in iframe
doc.body.appendChild(div)
注意:只有当 iframe 域与主页域相同时,您才能操作 iframe 内容
动态创建 iframe 示例:
var iframe = document.createElement('iframe'),
doc, div;
iframe.src = '' // same domain as main page
document.body.appendChild(iframe);
doc = iframe.contentDocument || iframe.contentWindow.document;
doc.open();
doc.write('some text') // if you want to write to the iframe
doc.close();
div = doc.createElement('div');
// this div will appear in iframe
div.appendChild(doc.createTextNode('im a div inside the iframe'));
doc.body.appendChild(div);
小提琴:here
【解决方案3】:
如果它没有 ID,你必须绘制它 DOM 路径,只需分析路径并在 JavaScript 或 JQUERY 中获取它