在HTML中,frameset元素是一个容器,用于将浏览器窗口划分为称为框架的矩形子空间。虽然用户代理仍然需要呈现它们,但在HTML5中,Framesets被认为是过时的。
如何使用 document.getElementById 在 iframe 中选取元素
我有一个像这样的iframe <html> <head></head> <frameset name="myframe2" cols="0%,...</desc> <question vote="92"> <p>我有一个像这样的<pre><code>iframe</code></pre></p> <pre><code><iframe name="myframe1" id="myframe1" width="100%" height="100%" src="a.html"> <html> <head></head> <frameset name="myframe2" cols="0%, 100%" border="0" frameBorder="0" frameSpacing="0"> <frame name="page1" src="c.html" scrolling="no"></frame> <frame name="page2" src="d.html" > <html> <head></head> <body id="top"> <div id="div1"> <div id="div2"> <div id="div3"> <ul id="x"> <li>a</li> <li>b</li> </ul> </div> </div> </div> </body> </html> </frame> </frameset> </html> </iframe> </code></pre> <p>我想引用元素“x”。我尝试了多种方法,但找不到解决方案。 </p> </question> <answer tick="true" vote="211"> <pre><code>document.getElementById('myframe1').contentWindow.document.getElementById('x') </code></pre> <p><a href="http://jsfiddle.net/qfRYQ/" rel="noreferrer"><strong>小提琴</strong></a></p> <p><a href="http://help.dottoro.com/ljctglqj.php" rel="noreferrer"><pre><code>contentWindow</code></pre></a> 所有浏览器都支持,包括旧版本的 IE。</p> <p>请注意,如果 <pre><code>iframe</code></pre> 的 <pre><code>src</code></pre> 来自其他域,则由于 <a href="http://en.wikipedia.org/wiki/Same_origin_policy" rel="noreferrer">同源策略</a>,您将无法访问其内容。</p> </answer> <answer tick="false" vote="25"> <p>使用 <a href="http://www.w3schools.com/jsref/prop_frame_contentdocument.asp" rel="noreferrer"><pre><code>contentDocument</code></pre></a> 来实现这一点</p> <pre><code>var iframe = document.getElementById('iframeId'); var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document; var ulObj = innerDoc.getElementById("ID_TO_SEARCH"); </code></pre> </answer> <answer tick="false" vote="8"> <p>(这是添加到所选答案中)</p> <p>确保 <pre><code>iframe</code></pre> 在您之前加载</p> <pre><code>contentWindow.document </code></pre> <p>否则,你的<pre><code>getElementById</code></pre>将是<pre><code>null</code></pre>。</p> <p>PS:无法发表评论,评论声誉仍然很低,但这是所选答案的后续,因为我花了一些很好的调试时间试图弄清楚我应该在选择内部之前强制加载<pre><code>iframe</code></pre> -iframe 元素。</p> </answer> <answer tick="false" vote="3"> <p>您需要确保框架已满载 最好的方法是使用 onload:</p> <pre><code><iframe id="nesgt" src="" onload="custom()"></iframe> function custom(){ document.getElementById("nesgt").contentWindow.document; } </code></pre> <p>当 iframe 完全加载时,此功能将自动运行。</p> </answer> <answer tick="false" vote="2"> <p>就我而言,我试图抓取 <strong>pdfTron</strong> 工具栏,但不幸的是,每次刷新页面时,它的 <strong>ID</strong> 都会发生变化。</p> <p>所以,我最终用这句话抓住了它:</p> <p><pre><code>const pdfToolbar = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('HeaderItems');</code></pre></p> <p>正如在 tagName 写入的数组中一样,您的应用程序中将始终拥有 iFrame 的固定索引。</p> </answer> <answer tick="false" vote="0"> <p>抱歉,我没有足够的声誉来添加评论,但在这种情况下也许写一个答案是可以的。我一直在寻找更改 Iframe 中某些 html 的 css。我有一个在 iframe 之外工作的书签代码,所以我就快到了:</p> <pre><code>javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementsByTagName('head'); caput[0].appendChild(style); })(); </code></pre> <p>我的 iframe 名为 <pre><code>xm-feed-iframe</code></pre></p> <p>所以我猜你会将代码更改为</p> <pre><code>document.getElementById('xm-feed-iframe').contentWindow.document.getElementById('x') </code></pre> <p>但是我需要使用一个类来更改 css 样式。所以我想这可能就像改变一样简单</p> <pre><code>var caput = document.getElementsByTagName('head') </code></pre> <p>到</p> <pre><code>var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head') </code></pre> <p>所以最终的代码是</p> <pre><code>javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head'); caput[0].appendChild(style); })(); </code></pre> <p>嘿,很快就成功了。我现在可以更深入地研究一下,并希望将一些计数器添加到 iframe 中出现的列表中! :)</p> <p>任何有关代码的评论将不胜感激,因为我是自学的!</p> <p>希望这对其他人有帮助</p> <p>谢谢</p> <p>更新: 我们在这里 - 我知道有一个使用书签的编号列表。我的生活现在将变得更加轻松,因为我将不再丢失我需要下载的所有项目。 :D</p> <p><a href="https://i.stack.imgur.com/tDVm0.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tL3REVm0wLnBuZw==" alt=""/></a></p> </answer> <answer tick="false" vote="0"> <p>我通过使用 .contentDocument 而不是 .contentWindow 解决了这个问题</p> </answer> </body></html>
我们的任务是创建一个网络程序作为测验,它将使用框架集和框架标签作为pdf页面导航器执行,我的问题是,在我单击锚点标签后,如果我单击锚点标签
我正在html(GUI)中使用框架集。我的GUI由四部分组成:页眉,页脚,侧面导航和内容页面。现在,我在页眉框架集中提供了用于注销按钮的功能。 ...
我有一个域parent.example,由提供者设置,用于将“带遮罩”转发给child.example。这意味着URL“ parent.example”保留在地址栏中,并且子级的内容。...
我有一个包含视频标签的简单HTML页面。当在框架集中显示在框架外时,我在视频上得到一个全屏工作按钮。当放置在框架集变体的框架内时,...
好的,所以我对 Javascript 很陌生,我想知道如何使背景颜色成为变量。具体来说,我有三个不同的框架,我想要侧面的两个
我想删除Framesets和Iframes,并且仍然可以独立页面刷新PHP数据,而无需刷新整个页面
我正在学习PHP,我正在尝试让我的新音频播放器在PHP中工作。那里的任何网页设计师可以帮我处理我的页面吗?我需要底部的玩家继续玩,而......
我需要隐藏页面源和控制台以导航framset src。或者整个页面。我想保护黑客域名的所在地。非常感谢你提前。 &...
我在这个网站上使用了selenium,但我不能使用它的任何元素。因为它们来自'frame'而且来自'frameset',这里是html部分;
如何使用java在selenium webdriver中的框架集中切换到框架集?
我发现的大部分答案都是关于切换到框架集内的框架。以下是脚本。
我正在使用scrapy + splash来为我的大学抓取网站。有些页面很古老,并且使用了我不熟悉的技术。我注意到有些网站没有完全渲染。所有不完整......