iframe使用总结(实战)

来源:互联网 发布:魔盒cms微信营销平台 编辑:程序博客网 时间:2024/06/10 19:04

说在前面的话,iframe是可以做很多事情的。
例如:
a>通过iframe实现跨域;
b>使用iframe解决IE6下select遮挡不住的问题
c>通过iframe解决Ajax的前进后退问题
d>通过iframe实现异步上传。(Easyui中form组件就是用的iframe,实现表单提交时,可以提交上传域)
下面就一些问题一一论述。

1、iframe基本知识:

iframe 元素会创建包含另外一个文档的内联框架(即行内框架)。
在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素。
提示:您可以把需要的文本放置在 和 之间,这样就可以应对无法理解 iframe 的浏览器。

可选属性:

标准属性:

2、操作iframe:

[html] view plain copy print?
01. 注:测试环境IE:8.0,FF:23.0.1
02. a>隐藏iframe表框
03. i>标签中设置:frameborder=”0”,
04. ii>DOM操作:
05.
06.
07.
08. var myiframe = document.getElementById("myiframe");
09. myiframe.style.border="none";//FF下有效,IE下无效
10. myiframe.setAttribute("frameborder",0);//FF下有效,IE下无效
11. myiframe.frameBorder = 0;//FF下有效,IE下无效

13.
14. b>动态创建iframe
15.
16. var newFrame = document.createElement("iframe");
17. newFrame.src ="http://blog.csdn.net/cuew1987";
18. newFrame.frameBorder = 0;//FF、IE隐藏边框有效
19. newFrame.width = "400px";
20. newFrame.height = "400px";
21. newFrame.scrolling = "no";
22. document.body.appendChild(newFrame);

24. c>获取iframe
25. i>var obj = document.getElementById(“iframeID”);
26. 获取iframe对象,可直接操作iframe标签属性,如只想改变iframe的 src 或者 border ,scrolling 等attributes
27. ii>var dom = frames[“iframeName”];
28. 获取iframe的DOM对象,此对象可用来操作对象,比如想操作iframe页面中的元素。
29. d>获取iframe中的window对象
30. function getIframeWindow(obj) {
31. //IE || w3c
32. return obj.contentWindow || obj.contentDocument.parentWindow;
33. //parentWindow 是 parent window object
34. }
35. document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:
36. IE:frames[id].document或obj.contentWindow.document;
37. FF:dom.contentDocument或obj.contentDocument;不绑定任何事件.
38.e>获取iframe页面高度
39. function getIframeHeight(obj){
40. var idoc = getIframeWindow(obj).document;
41. if(idoc.body){
42. return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);
43. }else if(idoc.documentElement){
44. return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);
45. }
46. }
47.f>父子页面互访
48. i>子访问父:
49. parent.html:
50.
51.

等到的信息:

52.
53.
54. son.html:
55.
56.
57.
58. function setMsg(){
59. var msg = window.parent.document.getElementById("msg");
60. msg.innerHTML= "Hello world!!";
61. }

63.
64. ii>父访问子:
65. parent.html:
66.
67.
等到的信息:

68.

69.
70.
71. function setMsg(){
72. var obj = document.getElementById("myiframe");
73. var msg = getIframeWindow(obj).document.getElementById("msg");
74. document.getElementById("setMsg").innerHTML = msg.innerHTML;
75. }

77.
78. son.html:
79.
80.
Hello world!!!

81.

3.iframe高度自适应和跨域:

[html] view plain copy print?
01.实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固定而显示出来的滚动条,不仅影响美观,还会对用户操作带来不便
02. a>同域下的高度自适应
03. parent.html:
04.
05.
06.
07. function getIframeWindow(obj) {
08. return obj.contentWindow || obj.contentDocument.parentWindow;
09. }
10. function getIframeHeight(obj){
11. var idoc = getIframeWindow(obj).document;
12. if(idoc.body){
13. return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);
14. }else if(idoc.documentElement){
15. return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);
16. }
17. }
18. function setHeight(){
19. var myiframe = document.getElementById("myiframe");
20. myiframe.height = getIframeHeight(myiframe);
21. }

23.
24. 另:document.documentElement与document.body相关说明(W3C DOM2.0规范)
25. document.doucmentElement:
26. documentElement of type Element, readonly,This is a convenience attribute that allows direct access to the
27. child node that is the root element of the document. For HTML documents, this is the element with the tagName “HTML”.
28. document.body:
29. document.body is the element that contains the content for the document. In documents with contents, returns the element,
30. and in frameset documents, this returns the outermost element.
31. Though body is settable, setting a new body on a document will effectively remove all the current children of the existing element.
32. IE在怪异模型(Quicks Mode)下document.documentElement无法正确取到clietHeight scrollHeight等值,比如clientHeight=0。
33. 获取scrollTop:
34. var sTop=Math.max(
35. (document.body?document.body.scrollTop:0),
36. (document.documentElement?document.documentElement.scrollTop:0),
37. (window.pageYOffset?window.pageYOffset:0)
38. );
39.
40. b>跨域下高度自适应
41. 页面:
42. index.html:(http://www.csdn.net)
43.
44. son.html:
45.
46.
47.
48.
49. function getHeight(){
50. var idoc = document;
51. if(idoc.body){
52. return Math.max(idoc.body.scrollHeight,idoc.body.offsetHeight);
53. }else if(idoc.documentElement){
54. return Math.max(idoc.documentElement.scrollHeight,idoc.documentElement.offsetHeight);
55. }
56. }
57. window.onload = function(){
58. var h = getHeight();
59. document.getElementById("agentIframe").src="http://www.csdn.net#"+h;
60. }

62. agent.html:(http://www.csdn.net)
63.
64. (function(){
65. var con = parent.parent.document.getElementById('frame_content');
66. var href = parent.parent.frames["frame_content"].frames["iframeC"].location.hash;
67. con.style.height = href.split("#")[1]+"px";
68. })();

4.iframe背景透明:

在ie6/7/8下引入iframe的时候,它的背景默认是白色,即使设置了style=”background-color:transparent;”也无效,
但是其他浏览器(firefox,chrome,opera,ie9)都正常显示,要解决这个兼容性问题,必须用到一个属性。
下面来看看现象:

[html] view plain copy print?
01.index.html:
02.
03.

0 0
原创粉丝点击