87,989
社区成员
发帖
与我相关
我的任务
分享
<body>
<iframe width="100%" height="490" frameborder="0" scrolling="no" onload="this.height=100" src="B.html" id="mainIframe" />
<script type="text/javascript">
var initIframe = function(){
var ifm= document.getElementById("mainIframe");
try{
var bHeight = ifm.contentWindow.document.body.scrollHeight;
var dHeight = ifm.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
ifm.height = height;
}catch (ex){
alert(ex.message);
}
}
window.setInterval("initIframe()",200); //iframe自适应高度
</script>
</body>
<head>
<script type="text/javascript">
window.onload = function(){document.getElementById("test").focus();};
</script>
</head>
<body>
<br />
......<!--很多个<br />不一一列出,保证页面有滚动条-->
<br />
<input type="text" id="test" />
</body>
//A页面处理
var initIframe = (function(){
var top = 0;
var temp = false;
return function() {
var ifm= document.getElementById("mainIframe");
try{
var bHeight = ifm.contentWindow.document.body.scrollHeight;
var dHeight = ifm.contentWindow.document.documentElement.scrollHeight;
if(ifm.contentWindow.document){
var subDoc = ifm.contentWindow.document;
if(top){
//alert(top);
if(document.documentElement && document.documentElement.scrollTop){
document.documentElement.scrollTop = top;
}else if(document.body){
document.body.scrollTop = top;
}
temp = true;
}else if(!top && !temp){
top = $(":focus:first",subDoc).offset().top;
}
}
var height = Math.max(bHeight, dHeight);
ifm.height = height;
}catch (ex){
alert(ex.message);
}
}
})();
window.setInterval("initIframe()",3000);
window.onload = function(){
document.getElementById("test").focus();
var $input = $("#test");
var top = $input.offset().top;
var ifm = parent.document.getElementById("mainIframe");
if ( $.browser.msie ) {
ifm.attachEvent( "onload", finish );
} else {
ifm.onload = $.browser.opera ? function(){ this.onload = finish; } : finish;
}
}
function finish(){
if(document.documentElement && document.documentElement.scrollTop){
document.documentElement.scrollTop = 0;
parent.document.documentElement.scrollTop = top;
}else if(document.body){
parent.document.body.scrollTop = top;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<div style="height:200px;">a</div>
<iframe width="100%" height="490" frameborder="0" scrolling="no" src="b.html" id="mainIframe" /></iframe>
<div style="height:200px;">b</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p>
<input type="text" id="test" />
<script type="text/javascript">
window.onload = function() {
parent.document.getElementById('mainIframe').style.height = document.documentElement.scrollHeight + 'px';
document.getElementById("test").focus();
}
</script>
</body>
</html>
//B页面
var timer,top;
window.onload = function(){
document.getElementById("test").focus();
var $input = $("#test");
top = $input.offset().top;
var ifm = parent.document.getElementById("mainIframe");
finish();
}
function finish(){
if(document.documentElement){
document.documentElement.scrollTop = 0;
parent.document.documentElement.scrollTop = top;
}else if(document.body){
parent.document.body.scrollTop = top;
}
if((parent.document.documentElement.scrollTop || parent.document.body.scrollTop) && timer){
clearTimeout(timer);
}else{
timer = setTimeout(finish,20);
}
}
