手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >iframe自适应宽度、高度 ie6 7 8,firefox 3.86下测试通过
iframe自适应宽度、高度 ie6 7 8,firefox 3.86下测试通过
摘要:废话不多说,贴上代码,也算是自己的一个代码存储。复制代码代码如下:vartemp_iframevarcontent=document.get...

废话不多说,贴上代码,也算是自己的一个代码存储。

复制代码 代码如下:

var temp_iframe

var content = document.getElementById('right'); //id为 right的DOM容器中,进行创建iframe和宽高自适应

var c = 0;

function append(filename)

{

var the_iframe = "helpfile" + c;

temp_iframe = document.createElement("iframe");

temp_iframe.src = filename;

temp_iframe.scrolling = "no";

temp_iframe.setAttribute("frameborder", "0");

temp_iframe.id = the_iframe;

temp_iframe.name = the_iframe;

scroll(0, 0);

content.innerHTML = "";

content.appendChild(temp_iframe);

if (document.all)

{

temp_iframe.attachEvent('onload', function()

{

temp_iframe.setAttribute("width", window.frames[the_iframe].document.body.scrollWidth); //自适应宽

temp_iframe.setAttribute("height", window.frames[the_iframe].document.body.scrollHeight); //自适应高

});

}

else

{

temp_iframe.addEventListener('load', function()

{

temp_iframe.setAttribute("width", window.frames[the_iframe].document.body.scrollWidth);

temp_iframe.setAttribute("height", window.frames[the_iframe].document.body.scrollHeight);

}, false);

}

c++;

return false;

}

/*调用方法

把此脚本另存为脚本文件:iframe.js,然后再前台页面中调用:

------------------------------------------------------------------------

<a href="index.html">

<div id="right"></div>

<script type="text/javascript" src="iframe.js"></script>

------------------------------------------------------------------------

或者 去掉脚本中最后的 return false,前台:

------------------------------------------------------------------------

<a href="javascript:append('favorite/Favorites.html')">

<div id="right"></div>

<script type="text/javascript" src="iframe.js"></script>

------------------------------------------------------------------------

*/

嘿嘿,其中,最后设置自适应宽高的,其实可以提出来成为一个函数来调用。

这个我没去那么做,有看到文章的朋友,可自行封装。

其中,还有一些不太方便的,比如我每次建立的iframe必须不同的名字和id,即时删除原来已经创建的也不行……

还是希望大家多多指教吧。

【iframe自适应宽度、高度 ie6 7 8,firefox 3.86下测试通过】相关文章:

浅析javascript函数表达式

JavaScript获得url查询参数的方法

JS实现两表格里数据来回转移的方法

使用Node.js处理前端代码文件的编码问题

javascript实现table选中的行以指定颜色高亮显示

如何实现iframe(嵌入式帧)的自适应高度

由浅到深了解JavaScript类

基于jquery实现下拉框美化特效

百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换

COOL而实用的动态效果

精品推荐
分类导航