2012-12-15 120 views
2

現狀:跳到IFRAME錨

第一:IFRAME ID爲miframe,顯示與錨網站叫素貞,即

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

二。父框架。

目標:使父框架內的鏈接看起來像

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

使在IFRAME內容滾動到錨俗塵。我的方法。

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。想法?

回答

5

通常你會使用一個鏈接像<a href="#your_anchor_name_or_id">Go to the anchor</a>。如果您想使用JavaScript來完成此操作,則可以更改window.location.hash的值。下面的代碼應該做的框架內:

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

如果你不想改變的哈希值,有an example on MDN。您可以修改它的iframe。

2

下面的代碼應該滾動iframe來錨點的位置:

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

編輯:

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

+0

沒有嘗試...但我在想,你應該通過它的所有'offsetParent's環和總結自己的'offsetTop's。 –

+0

沒有工作.... –