跳转到 iframe 中的锚点 [英] Jumping to anchor in iframe

查看:63
本文介绍了跳转到 iframe 中的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

第一个:iframe,id为miframe,显示一个带有名为suchen的锚点的站点,即

First: iframe with id miframe, displaying a site with the anchor called suchen, i.e.

<div id="suchen"></div>. 

第二.父框架.

目标:在父框架内建立一个看起来像

Goal: Make a link within the parent frame looking like

<a class="LINK0" title="" href="javascript:springen('suchen');">w/e</a> 

使 iframe 中的内容滚动到锚点 suchen.我的方法.

make the content in the iframe be scrolled to the anchor suchen. My approach.

function springen(anker) { 
    var childWindow =  document.getElementById("miframe").contentWindow;

    // this should emulate a click on the ptAnchor DIV's child A HREF.
    //childWindow.document.getElementById(anker).firstChild.click();
    //childWindow.document.getElementById(anker).scrollIntoView();
    // alternatively, this will simply scroll the child window to the top-left corner
    //childWindow.scrollTo(0,200);
}

childWindow.scrollTo(0,200); 到目前为止有效,滚动发生在 200.但是我需要滚动到锚点,无论它在 iframe 中的哪个位置.想法?

childWindow.scrollTo(0,200); works in so far, that scroll happens to 200. But I need scrolling to the very anchor, wherever it is in the iframe. Ideas?

推荐答案

通常你会使用像<a href="#your_anchor_name_or_id">转到锚点</a>这样的链接.如果您想使用 JavaScript 执行此操作,您可以更改 window.location.hash 的值.以下代码应该在一个框架内完成:

Normally you would use a link like <a href="#your_anchor_name_or_id">Go to the anchor</a>. If you want to do this using JavaScript, you can change the value of window.location.hash. The following code should do that within a frame:

document.getElementById("the_id_of_your_iframe").contentWindow.location.hash = "your_anchor_name_or_id";

如果你不想改变哈希值,有MDN 上的一个例子.您可以针对 iframe 对其进行修改.

If you don't want to change the hash value, there is an example on MDN. You can modify it for an iframe.

这篇关于跳转到 iframe 中的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆