【问题标题】:Jquery iframe to find and replace contentjquery iframe 查找和替换内容
【发布时间】:2025-12-28 02:25:16
【问题描述】:

我有一个单页网站,其中包含一个 iframe,当 iframe 加载时,它包含一个我想隐藏的表,并且该表还在多个页面上重复,所以我想在我的主页页眉中添加一个代码这个内容来自 iframe 并隐藏它,即使 iframe 加载新页面我想要隐藏的代码是

<table cellSpacing="0" cellPadding="0" border="0">
<tr>
<td width="225"><a id="_ctl0_ucMNB_logo_LogoHyperLink" href="#"><img id="_ctl0_ucMNB_logo_imgLeftPart" class="header_logo_img" src="#" border="0" /></a></td>
</tr>
</table>

【问题讨论】:

  • 参考这篇文章:*.com/questions/364952/…? rq=1
  • iFrame src和主页面在同一个域吗?
  • 你应该紧急改进你的问题,让它更清楚......
  • 如果你给表格一个特定的id,你可以像下面的答案一样选择iframe的内容,然后从中选择表格并删除它:.contents().$('#idyougavetothetable').remove()

标签: javascript jquery html iframe


【解决方案1】:

可以使用.contents()获取iframe的内容并对其进行操作, 更多细节 https://api.jquery.com/contents/

jQuery, select element inside iframe, which is inside an iframe

【讨论】:

  • &lt;script type="text/javascript"&gt; setInterval(function(){ $(document).ready(function(){ var f=$('body') f.load(function(){ f.contents().find('#_ctl0_ucMNB_logo_LogoHyperLink').html("&lt;h1 style='text-align: center;'&gt;This IS an iframe&lt;/h1&gt;"); f.contents().find( "#_ctl0_bodyTag" ).css( "background-image", "none" ); }) }) }, 5000); &lt;/script&gt; 即使在 iframe 加载新页面后点击链接此代码也不起作用请帮助
  • @user1393638,你能在jsfiddle.net你的代码中做一个小提琴,看看
  • 这是我的小提琴jsfiddle.net/C9Ly4,当我们点击 iframe 中存在的某个链接时,它会加载新页面,其中包含我想要删除的 div #_ctl0_ucMNB_logo_LogoHyperLink 和 #_ctl0_bodyTag ,但此代码已完成删除这些标签请帮助我
【解决方案2】:

$('iframe').contents().find('control');

【讨论】: