如何更改此iframe内容?

时间:2013-11-05 23:47:44

标签: javascript jquery html iframe

示例主页,例如 index.html 内容:

<iframe src="a.html" width="400" height="300" frameborder="1" id="frame"></iframe>

a.html 内容:

<html><body><form >
<div id="container"><div id="main"><div class="red_bl"><div class="red_br">
<div class="red_tl"><div class="red_tr"><div id="content">

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody id="subContent1">
<tr>another tags</tr>

<tr>
<td colspan="2" class="cell2" valign="top">
sample text
</td>
<td id="td_address" class="cell3">sample code</td>
</tr>

</tbody>
</table>


</div></div></div></div></div></div></div>
</form></body></html>

我想要改变:

<td id="td_address" class="cell3">sample code</td>

例如:

<td id="td_address" class="cell3">another text</td>

通过jQuery在 index.html

2 个答案:

答案 0 :(得分:1)

使用JQuery:

$('#frame').contents().find('td#td_address').html('another text');

答案 1 :(得分:1)

这是因为您的document.ready并未等待iframe加载。

因此,您运行的代码将在#td_address添加到DOM之前执行。

对于此问题的各种解决方案(取决于您能够更改的内容),请查看this question的答案。

相关问题