问题描述
情况:
第一步: iframe miframe ,显示一个名为 suchen ,即
< div id =suchen>< / div> ;.
其次。父框架。
目标:在父框架中建立一个链接,看起来像
< a class =LINK0title =href =javascript:springen('suchen');> w / e< / a>
使 iframe中的内容滚动到锚点素贞。我的方法。
pre $函数springen $ an $ b $ var childWindow = document.getElementById(miframe)。contentWindow ;
//这应该模拟点击ptAnchor DIV的子A HREF。
//childWindow.document.getElementById(anker).firstChild.click();
//childWindow.document.getElementById(anker).scrollIntoView();
//或者,这将简单地将子窗口滚动到左上角
//childWindow.scrollTo(0,200);
}
childWindow.scrollTo(0,200);
到目前为止工作,滚动发生到200.
但我需要滚动到非常锚点,无论它在 iframe 中。想法?
通常情况下,您会使用< a href =#your_anchor_name_or_id> ;转到锚< / a>
。如果你想使用JavaScript来做到这一点,你可以改变 window.location.hash
的值。以下代码应该在一个框架内完成:
document.getElementById(the_id_of_your_iframe)。contentWindow.location.hash = your_anchor_name_or_id;
如果您不想更改散列值,那么。您可以修改它的iframe。
Situation:
First: iframe with id miframe, displaying a site with the anchor called suchen, i.e.
<div id="suchen"></div>.
Second. Parent frame.
Goal: Make a link within the parent frame looking like
<a class="LINK0" title="" href="javascript:springen('suchen');">w/e</a>
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);
works in so far, that scroll happens to 200.But I need scrolling to the very anchor, wherever it is in the iframe. Ideas?
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";
If you don't want to change the hash value, there is an example on MDN. You can modify it for an iframe.
这篇关于跳转到锚定在iframe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!