简体   繁体   English

使用从父页面到iframe的跳转链接

[英]Using Jump Links from Parent Page to an Iframe

I have a parent page that contains an iFrame. 我有一个包含iFrame的父页面。 The anchors have that have a respective The anchors are outside of the iframe but I want them to be able to work as page jump links. 锚点具有各自的锚点。这些锚点位于iframe之外,但我希望它们能够用作页面跳转链接。

I've tried adding and targeting the iframe but it still does not work for me. 我曾尝试添加并定位iframe,但它仍然对我不起作用。

Any suggestions or recommendations? 有什么建议或建议吗?

In order to target an element inside of the iframe, you'll want to use Javascript and target it like so: 为了定位iframe中的某个元素,您需要使用Javascript并将其定位为:

function findText() { 
    var frame =  document.getElementById('iframe').contentWindow;
    frame.scrollTo(0,frame.document.getElementById('suchen').offsetTop);
}

Then your markup will be something like this: 然后,您的标记将如下所示:

<iframe src="http://fiddle.jshell.net/pkvhw/2/show/" id="iframe"></iframe>
<a href="javascript:findText();">Find Text</a> 

Where iframe is the id of the iframe on your page, and suchen is the id of the element you want to find within the iframe. 其中, iframe是页面上iframe的ID, suchen是要在iframe中找到的元素的ID。

Here is a fiddle of it in action: http://jsfiddle.net/vs9xhs0o/ 这是操作中的一个小提琴: http : //jsfiddle.net/vs9xhs0o/

Note: You will run into issues when trying to use this going across domains. 注意:尝试跨域使用此功能时,您会遇到问题。 Most sites will not allow you to make cross-domain requests like this, so make sure you have control over the site you are viewing through the iframe. 大多数网站都不允许您这样发出跨域请求,因此请确保您可以控制通过iframe查看的网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM