Html 如何定位 iframe 内的 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8501595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I target a div inside an iframe?
提问by Sativa Diva
I have an element inside an <iframe>
I'd like to activate using a link outside the <iframe>
.
我有一个元素,<iframe>
我想使用<iframe>
.
Example:
例子:
<iframe src="iframe.html" id="iframe">
*inside the iframe*
<div id="activate" class="activate">
</iframe>
<a href="#activate" target="iframe">Link</a>
I thought this would work but obviously does nothing because I'm stupid. Any ideas?
我认为这会奏效,但显然什么也没做,因为我很愚蠢。有任何想法吗?
回答by Rob W
Framed page (test.html
):
框架页 ( test.html
):
....... lots of content ....
<div id="activate">Inside frame</div>
Page containing the frame (page-containing-frame.html
):
包含框架 ( page-containing-frame.html
) 的页面:
<iframe src="test.html" name="target-iframe"></iframe>
<a href="test.html#activate"
target="target-iframe"
onclick="frames['target-iframe'].document.getElementById('activate')
.scrollIntoView();return false">Click</a>
^ That's the link. I've split up code over multiple lines for visibility
Explanation
解释
- The frame has a
name
attrbutewith the value oftarget-iframe
(obviously, you can choose any desired value). The link contains three attributes, each supporting two methods to scroll to a link in the frame:
target="target-iframe"
andhref="test.html#activate"
This is the fallback method, in case of an error occurs, or if the user has disabled JavaScript.
The target of the link is the frame, thehref
attribute mustbe the path of the frame, postfixed by the anchor, egtest.hrml#activate
. This method will cause the framed page to reload. Also, if the anchor is already at#activate
, this method will not work any more.- This is the elegant solution, which shold not fail. The desired frame is accessed through the global
frames
object (by name, NOT by id,target-iframe
). Then, the anchor is selected (document.getElementById('activate')
.
Finally, thescrollIntoView
method is used to move the element inside the viewport.
Theonclick
method ends withreturn false
, so that the default behaviour (ie following the link, causing a page refresh), does not happen.
- 该框架具有值为 的
name
属性target-iframe
(显然,您可以选择任何所需的值)。 链接包含三个属性,每个属性都支持两种方法来滚动到框架中的链接:
target="target-iframe"
并且href="test.html#activate"
这是回退方法,以防发生错误,或者如果用户禁用了 JavaScript。
链接的目标是框架,href
属性必须是框架的路径,后缀为锚点,例如test.hrml#activate
。此方法将导致重新加载框架页面。此外,如果锚点已经在#activate
,则此方法将不再起作用。- 这是一个优雅的解决方案,它不会失败。通过全局
frames
对象(按名称,而不是 id,target-iframe
)访问所需的帧。然后,选择锚点 (document.getElementById('activate')
。
最后,该scrollIntoView
方法用于在视口内移动元素。
该onclick
方法以 结尾return false
,因此不会发生默认行为(即跟随链接,导致页面刷新)。
Your current code did not work, because of the missing name
attribute (target="..."
cannot match IDs, only names). Also, #activate
is parsed in the context of the current page, so, the link points to page-containing-frame.html
.
您当前的代码不起作用,因为缺少name
属性(target="..."
无法匹配 ID,只能匹配名称)。此外,#activate
在当前页面的上下文中解析,因此,链接指向page-containing-frame.html
。