jquery获取、设置iframe高度

来源:互联网 发布:海思 网络摄像机 sdk 编辑:程序博客网 时间:2024/05/22 01:53


今天网页加了个iframe  通过js制定src,但是当获取iframe的高度时,始终显示0

可以在iframe的width属性设置height和width,

但是动态设置,是iframe,自适应高度,应在iframe每次加载时,load完成之后根据内容改变

  1. $(function(){   
  2.      $("#iframe").load(function(){          
  3.          $(this).height($(this).contents().find("#content").height() + 400);   
  4.      });   
  5.        
  6. });


这里的find("#content")是找出iframe内容文档中的id为content的高度(另外比如find("body")),并设置给iframe, 
类似的还可以设置宽度

  1. $(function(){   
  2.      $("#ifram").load(function(){        
  3.          var height = $(this).contents().find("#box").height() + 40;   
  4.          $(this).height( height < 400 ? 400 : height );   
  5.      });   
  6.        
  7. });     

原创粉丝点击