最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jumping to anchor in iframe - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Dec 15, 2012 at 15:16 Rob 438k74 gold badges836 silver badges1.1k bronze badges asked Dec 15, 2012 at 14:56 Karl HeinzKarl Heinz 651 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

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.

The following code should scroll the iframe to the anchor's position:

function springen(anker) { 
    var childWindow =  document.getElementById("miframe").contentWindow;
    childWindow.scrollTo(0,childWindow.document.getElementById('suchen').offsetTop);
}

Edit:

JSFiddle: http://jsfiddle.net/pkvhw/6/

发布评论

评论列表(0)

  1. 暂无评论