龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

JS实现跨域访问iframe里的dom(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
三、重写iframe里的内容 通过designMode(设置文档为可编辑设计模式)和contentEditable(设置内容为可编辑),你可以重写iframe里的内容。代码: variObj = docu

三、重写iframe里的内容

通过designMode(设置文档为可编辑设计模式)和contentEditable(设置内容为可编辑),你可以重写iframe里的内容。代码:

  1. variObj=document.getElementById(‘iId‘).contentWindow;  
  2. iObj.document.designMode=‘On‘;  
  3. iObj.document.contentEditable=true;  
  4. iObj.document.open();  
  5. iObj.document.writeln(‘<html><head>‘);  
  6. iObj.document.writeln(‘<style>body{background:#000;  
  7. font-size:9pt;margin:2px;padding:0px;}</style>‘);  
  8. iObj.document.writeln(‘</head><body></body></html>‘);  
  9. iObj.document.close(); 

firebug测试以上代码性能,如图

注释掉

  1. iObj.document.designMode=‘On’;  
  2.  
  3. iObj.document.contentEditable=true;  
  4.  

结果如图:

效果没有变,时间效率是注释前的将近三倍。嘿嘿。那两个对象是参考网络一些人的写法,重写iframe里内容,其实没有必要用designMode和contentEditable,除非有其他的需求。

四、iframe自适应高度

有了上面的原理要实现这个相当简单,就是把iframe的height值设置成它里面文档的height值就可以。代码:

  1. window.onload=(function(){  
  2. variObj=document.getElementById(‘iId‘);  
  3. iObjiObj.height=iObj.  
  4. contentWindow.document.documentElement.scrollHeight;  
  5. }); 

精彩图集

赞助商链接