2012-12-15 3 views
2

상태를 고정한다. 부모 프레임.점핑 iframe에

목표 : 상위 프레임에 포함 된 링크가

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

처럼 보이는 확인은 iframe을의 내용은 앵커 suchen로 스크롤합니다. 내 접근 방식. 지금까지의

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을에 어디든지 나는 매우 앵커로 스크롤 필요가 없다. 아이디어? 앵커의 위치로 iframe을 스크롤해야

답변

5

http://jsfiddle.net/pkvhw/6/는 일반적으로 당신이 <a href="#your_anchor_name_or_id">Go to the anchor</a> 같은 링크를 사용합니다. 자바 스크립트를 사용하여이 작업을 수행하려는 경우 window.location.hash 값을 변경할 수 있습니다. 다음 코드는 프레임 내에서 그렇게해야합니다

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

당신이 해시 값을 변경하지 않으려면, an example on MDN있다. iframe에 맞게 수정할 수 있습니다.

2

다음 코드 :

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

편집 :

JSFiddle :

+0

시도하지 않고 ...하지만 나는 당신이 모든 offsetParent를 반복하고 그들의 offsetTop을 합산해야한다고 생각했습니다. –

+0

작동하지 않았습니다 .... –