I have an iframe window that displays a report. To access this iframe, I have an expand/collapse button on the parent window.
我有一个显示报告的iframe窗口。要访问此iframe,我在父窗口上有一个展开/折叠按钮。
Within the iframe report window, I have a #PAGETOP anchor at the bottom of the report, i,e:
在iframe报告窗口中,我在报告的底部有一个#PAGETOP锚点,我:
<tr><td style="padding-left:45px" class="bottom" colspan="99"><div id="ptAnchor"><a href="#PAGETOP"><img src="first.png" border="1" alt="Top of Page" /></a></div></td></tr>
How can I programatically emulate an onclick of this #PAGETOP anchor, when the user presses the "expand" button from the parent window?
当用户从父窗口按下“展开”按钮时,如何以编程方式模拟此#PAGETOP锚点的onclick?
3 个解决方案
#1
The use of #PAGETOP
anchor is to scroll the window to the top. You can instead write a function to scroll the window to 0,0 coordinates.
#PAGETOP锚点的使用是将窗口滚动到顶部。您可以改为编写一个函数来将窗口滚动到0,0坐标。
function ScrollToTop() {
window.scroll(0, 0);
}
Put this function in the IFrame, and call it from the parent window.
将此函数放在IFrame中,并从父窗口调用它。
#2
In the parent doc:
在父文档中:
<a id="scrollToTop">Expand</a>
<script type="text/javascript">
document.getElementById("scrollToTop").onclick = function(e) {
var childWindow = document.getElementById("theIFrame").contentWindow;
// this should emulate a click on the ptAnchor DIV's child A HREF.
childWindow.document.getElementById("ptAnchor").firstChild.click();
// alternatively, this will simply scroll the child window to the top-left corner
childWindow.scrollTo(0,0);
}
</script>
<iframe id="theIFrame" src="someDocOnTheSameDomain.html"></iframe>
Your iframe must be on the same domain for the browser to allow scripting between the parent and child frames.
您的iframe必须位于浏览器的同一域中,以允许父帧和子帧之间的脚本。
The contentWindow property allows you to access the window object of the child iframe. From there, you can write Javascript to emulate a click or simply scroll the window around.
contentWindow属性允许您访问子iframe的window对象。从那里,您可以编写Javascript来模拟点击或只是滚动窗口。
#3
You can do something like this:
你可以这样做:
<iframe name="foo"></iframe>
<button>Expand</button>
<script type="text/javascript">
$(function(){
$('button').click(function(){
window.frames['foo'].location.hash = "top";
});
});
</script>
#1
The use of #PAGETOP
anchor is to scroll the window to the top. You can instead write a function to scroll the window to 0,0 coordinates.
#PAGETOP锚点的使用是将窗口滚动到顶部。您可以改为编写一个函数来将窗口滚动到0,0坐标。
function ScrollToTop() {
window.scroll(0, 0);
}
Put this function in the IFrame, and call it from the parent window.
将此函数放在IFrame中,并从父窗口调用它。
#2
In the parent doc:
在父文档中:
<a id="scrollToTop">Expand</a>
<script type="text/javascript">
document.getElementById("scrollToTop").onclick = function(e) {
var childWindow = document.getElementById("theIFrame").contentWindow;
// this should emulate a click on the ptAnchor DIV's child A HREF.
childWindow.document.getElementById("ptAnchor").firstChild.click();
// alternatively, this will simply scroll the child window to the top-left corner
childWindow.scrollTo(0,0);
}
</script>
<iframe id="theIFrame" src="someDocOnTheSameDomain.html"></iframe>
Your iframe must be on the same domain for the browser to allow scripting between the parent and child frames.
您的iframe必须位于浏览器的同一域中,以允许父帧和子帧之间的脚本。
The contentWindow property allows you to access the window object of the child iframe. From there, you can write Javascript to emulate a click or simply scroll the window around.
contentWindow属性允许您访问子iframe的window对象。从那里,您可以编写Javascript来模拟点击或只是滚动窗口。
#3
You can do something like this:
你可以这样做:
<iframe name="foo"></iframe>
<button>Expand</button>
<script type="text/javascript">
$(function(){
$('button').click(function(){
window.frames['foo'].location.hash = "top";
});
});
</script>