框架高度自适应

一、父窗口代码
<iframe id="_case" src="case.html" width="300" frameborder="0" scrolling="no" ></iframe>


二、子窗口代码
<div id="relation" style="padding-top:10px;" >
文字内容
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function() {
    var h=$("#relation").height()+30;
    $(window.parent.document).find("iframe[id='_case']").height(h);
});
</script>



备注【var h=$("#relation").height()+30;】的  +30 是ifarme部分其他元素的height

-------------------------下面是自己写的实例-------------------------

【index.html   、 1.html   、  2.html   、 3.html 、jquery-1.9.1.min.js 都在同一目录下】

----------------------------------------index.html----------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>index.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <script src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    window.onload = function()
    {
        
        $("#butt1").mouseover(function()
        {
            $("#_case").attr("src","1.html");
            
        }).mouseout(function()
        {
            
        })
        
      $("#butt2").mouseover(function()
        {
            $("#_case").attr("src","2.html");
        }).mouseout(function()
        {
            
        })

        $("#butt3").mouseover(function()
        {
            $("#_case").attr("src","3.html");
        }).mouseout(function()
        {
            
        })
    }
</script>
  </head>
 
  <body>
    <div>
    
    <input type="button" id="butt1" value="butt1" />
    <input type="button" id="butt2" value="butt2" />
    <input type="button" id="butt3" value="butt3" />
    </div>
    <iframe id="_case" src="/1.html" width="300" frameborder="0" scrolling="no" >
    </iframe>
    <div>
    iiiiiiii<br/>
    iiiiiiiiiiii<br/>
    iiiiiiiii<br/>
    iii<br/>
    iiiiiiii<br/>
    i2<br/>
    iiiiiiiiiiiiiiiiiiiiiiii<br/>
    iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii<br/>
    </div>
  </body>
</html>

---------------------------------------------1.html-----------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>1.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script src="jquery-1.9.1.min.js"></script>
    <script language="javascript">
    $(document).ready(function() {
        var h=$("#relation").height()+30;
        $(window.parent.document).find("iframe[id='_case']").height(h);
    });
    </script>
  </head>
 
  <body>
    <div id="relation" style="padding-top:10px; background-color: red;" >
    文字内容
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    111<br/>
    </div>
    
  </body>
</html>


------------------------------------------------2.html--------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>2.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script src="jquery-1.9.1.min.js"></script>
    <script language="javascript">
    $(document).ready(function() {
        var h=$("#relation").height()+30;
        $(window.parent.document).find("iframe[id='_case']").height(h);
    });
    </script>
  </head>
 
  <body>
  <div id="relation" style="padding-top:10px; background-color: red;" >
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
    222<br/>
  </div>
  </body>
</html>


-------------------------------------------------3.html-------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
  <head>
    <title>3.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <script src="jquery-1.9.1.min.js"></script>
    <script language="javascript">
    $(document).ready(function() {
        var h=$("#relation").height()+30;
        $(window.parent.document).find("iframe[id='_case']").height(h);
    });
    </script>
  </head>
 
  <body>
  <div id="relation" style="padding-top:10px; background-color: red;" >
    333<br>
    333<br>
    333<br>
    333<br>
    333<br>
  </div>
  </body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值