五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > iframe元素用法總結(jié)

iframe元素用法總結(jié)

文章來源:365jz.com     點(diǎn)擊數(shù):190    更新時間:2015-09-16 20:13   參與評論

iframe是一個非常有用的標(biāo)簽,先不說早期用它來模型Ajax效果,現(xiàn)在富本文編輯器它也絕對是主角。但是它又是一個特別的元素,最早出現(xiàn)IE4.0中,后紛紛被其他游覽器吸納,由于IE不開源,iframe在各游覽器中都有很大差異。首先我們看看它在各游覽器中擁有什么屬性——

火狐的iframe的所有屬性:

scrollWidth, clientLeft, clientHeight, clientWidth, clientTop, getClientRects, getBoundingClientRect, getElementsByClassName, baseURI, textContent, compareDocumentPosition, getUserData, isSameNode, lookupNamespaceURI, setUserData, lookupPrefix, isDefaultNamespace, isEqualNode, dispatchEvent, removeEventListener, style,contentEditable, offsetParent, innerHTML, offsetLeft, offsetTop, offsetHeight, offsetWidth, contentWindow, src, contentDocument, className, id, title, tagName, removeAttributeNS, removeAttribute, getAttribute, getElementsByTagName, setAttribute, getElementsByTagNameNS, hasAttributeNS, setAttributeNS, hasAttribute, getAttributeNS, nextSibling, firstChild, prefix, nodeValue, childNodes, nodeName, namespaceURI, previousSibling, nodeType, localName, lastChild, ownerDocument, parentNode, attributes, appendChild, cloneNode, normalize, hasChildNodes, insertBefore, replaceChild, removeChild, hasAttributes, isSupported, getAttributeNode, setAttributeNode, removeAttributeNode, getAttributeNodeNS, setAttributeNodeNS, ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, ENTITY_REFERENCE_NODE, ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, DOCUMENT_FRAGMENT_NODE, NOTATION_NODE, lang, dir, align, frameBorder, height, longDesc, marginHeight, marginWidth, name, scrolling,width , getSVGDocument, tabIndex, draggable, blur, focus, scrollIntoView, spellcheck, addEventListener, getFeature, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_PRECEDING, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, scrollTop, scrollLeft, scrollHeight, firstElementChild, lastElementChild, previousElementSibling, nextElementSibling, childElementCount, children, querySelector, querySelectorAll

IE6的iframe所有屬性

language, scrollHeight, isTextEdit, currentStyle,document , onmouseup, oncontextmenu, isMultiLine, clientHeight, onrowexit, onbeforepaste, onactivate, scrollLeft, lang, onmousemove, onmove, onselectstart, parentTextEdit, oncontrolselect, canHaveHTML, onkeypress, oncut, onrowenter, onmousedown, onpaste, className, id, onreadystatechange,onbeforedeactivate , hideFocus, dir, isContentEditable, onkeydown, clientWidth, onlosecapture, parentElement, ondrag, ondragstart, oncellchange, recordNumber, onfilterchange, onrowsinserted, ondatasetcomplete, onmousewheel, ondragenter, onblur, onresizeend, onerrorupdate, onbeforecopy, ondblclick, scopeName, onkeyup, onresizestart, onmouseover, onmouseleave, outerText, innerText, onmoveend, tagName, title, offsetWidth, onresize, contentEditable, runtimeStyle, filters, ondrop, onpage, onrowsdelete, tagUrn, offsetLeft, clientTop, style, onfocusout, clientLeft, ondatasetchanged, canHaveChildren, ondeactivate, isDisabled, onpropertychange, ondragover, onhelp, ondragend, onbeforeeditfocus, disabled, onfocus, behaviorUrns, accessKey, onscroll, onbeforeactivate, onbeforecut, readyState, all, sourceIndex, onclick, scrollTop, oncopy, onfocusin, tabIndex, onbeforeupdate, outerHTML, innerHTML, ondataavailable, offsetHeight, onmovestart, onmouseout, scrollWidth, offsetTop, onmouseenter, onlayoutcomplete, offsetParent, onafterupdate, ondragleave, children, parentNode, border, nodeValue, firstChild, name, align, marginWidth,contentWindow , hspace, frameSpacing, dataFormatAs, lastChild, ownerDocument, vspace, marginHeight, dataFld, attributes, dataSrc, src, frameBorder, nodeType, noResize, width, previousSibling, scrolling, nodeName, childNodes, longDesc, onload, nextSibling, height, allowTransparency

IE8的iframe所有屬性

nextSibling, onresizeend, onrowenter, aria-haspopup, childNodes, ondragleave, canHaveHTML, onbeforepaste, ondragover, onbeforecopy, aria-disabled, onpage, recordNumber, previousSibling, nodeName, onbeforeactivate, accessKey, currentStyle, scrollLeft, onbeforeeditfocus, oncontrolselect, aria-hidden, onblur, hideFocus, clientHeight,style, onbeforedeactivate, dir, aria-expanded, onkeydown, nodeType, ondragstart, onscroll, onpropertychange, ondragenter, id, aria-level, onrowsinserted, scopeName, lang, onmouseup, aria-busy, oncontextmenu, language, scrollTop, offsetWidth, onbeforeupdate, onreadystatechange, onmouseenter, filters, onresize, isContentEditable, aria-checked, aria-readonly, oncopy, onselectstart, scrollHeight, onmove, ondragend, onrowexit, lastChild, aria-secret, onactivate, canHaveChildren, onfocus, onfocusin, isMultiLine, onmouseover, offsetTop, oncut, parentNode, tagName, className, onmousemove, title, role, behaviorUrns, onfocusout, onfilterchange, disabled, parentTextEdit, ownerDocument, offsetParent, aria-posinset, ondrop, ondblclick, onrowsdelete, tabIndex, onkeypress, aria-relevant, onlosecapture, innerText, aria-live, parentElement, ondeactivate, aria-labelledby, aria-pressed, children, ondatasetchanged, ondataavailable, aria-invalid, onafterupdate, nodeValue, onmousewheel, onkeyup, readyState, onmovestart, aria-valuenow, aria-selected, onmouseout, aria-owns, aria-valuemax, onmoveend, contentEditable, document, firstChild, sourceIndex, outerText, isTextEdit, isDisabled, oncellchange, runtimeStyle, scrollWidth, aria-valuemin, onlayoutcomplete, onhelp, attributes, offsetHeight, onerrorupdate, onmousedown, clientTop, aria-setsize, clientWidth, onpaste, tagUrn, onmouseleave, onclick, outerHTML, ondrag, aria-controls, onresizestart, aria-flowto, ondatasetcomplete, aria-required, clientLeft, aria-describedby, all, onbeforecut, innerHTML, aria-activedescendant, aria-multiselectable, offsetLeft, frameSpacing, vspace, noResize, onload,height , marginHeight,border , longDesc, src, frameBorder, dataFormatAs,width , dataSrc, allowTransparency, contentDocument, name,contentWindow, marginWidth, dataFld, scrolling, align, hspace

通常要隱藏iframe的邊框時需要用到frameborder屬性,例如使用Dreamweaver可以生成如下代碼:

1.
2.<iframe frameborder=0 src='xxxx' width='xxx' height='xxx'></iframe>

但是如果使用DOM方式來生成一個iframe時IE卻始終隱藏不了邊框,例如:

1.
2.var iframe = document.createElement('iframe');
3.iframe.setAttribute('frameborder',0);//Firefox下有效,IE下無效

我們需要直接對其屬性進(jìn)行賦值:

 

1.
2.iframe.frameBorder=0;//Firefox和IE均有效

Internet Explorer 5.5 支持浮動框架的內(nèi)容透明。如果想要為浮動框架定義透明內(nèi)容,則必須滿足下列條件:

  • 與 iframe 元素一起使用的 allowTransparency 標(biāo)簽屬性必須設(shè)置為 true。
  • 在 iframe 內(nèi)容源文檔,background-color 或 body 元素的 bgColor 標(biāo)簽屬性必須設(shè)置為 transparent。

1. 包含框架頁的代碼。

1.
2.<body bgColor="#eeeeee"> <iframe allowTransparency="true" src="transparent.htm"> </iframe>

2.transparent.htm頁的代碼。

1.
2.<body bgColor="transparent">

 

對iframe進(jìn)行操作:

1.
2.var obj = document.getElementById(“iframe”);//獲取對象
3.var dom = document.all.frames[“iframe”];//獲取DOM

如果只想改變iframe的 src 或者 border ,scrolling 等attributes(與property不是一個概念,property是不能寫在標(biāo)簽內(nèi)的,比如:scrollHeight,nnerHTML等),就需要用到第一種方法。

如果想取得iframe的頁面(不是iframe本身)。就需要使用第二種方法,因?yàn)樗〉玫氖且粋€完整的DOM模型,比如想得到iframe的document.body的內(nèi)容,就只能用第二種方法

操作iframe中的DOM元素,需要注意以下兩點(diǎn):

  1. 必須先獲取指定iframe的document
  2. 對于1,必須在頁面load完以后才能獲??;

 

控制Iframe中的頁面的樣式

01.
02.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
03.<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
04.<head>
05.<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
06.<title></title>
07.</head>
08.<body>
09.<iframe id="FF" src="B.htm"></iframe>
10.<input type="submit" value=" OK " onclick="OK()"/>
11.
12.<script language="JavaScript">
13.function OK()
14.{
15. // 操作iframe中內(nèi)容的CSS
16. var iframe = document.getElementById('FF');
17. var body = iframe.contentWindow.document.body;
18.
19. body.style.marginTop = 50;
20. body.style.padding = 0;
21. body.style.fontSize = 30;
22. body.style.textAlign = 'center';
23. body.style.backgroundColor = 'red';
24. body.style.color = 'blue';
25.}
26.</script>
27.</body>
28.</html>

去掉動態(tài)創(chuàng)建的iframe的邊框

1.
2.var iframe = document.createElement('iframe');
3.iframe.scrolling="no";//這兩個屬性的設(shè)置缺一不可。
4.iframe.frameborder="no";

window跟iframe標(biāo)簽是兩回事 iframe的contentWindow屬性是它里面的window

用getElementById才能取到iframe標(biāo)簽 ie直接用id得到的是里面的window

有關(guān)高度自適應(yīng)問題

scrollHeight返回的是一個數(shù)字,沒有帶單位

在IE 下如果沒有DTD聲明,則按loose.dtd 解析,在設(shè)置對像的寬高時,會自動的加上單位"px"

將iframe的高度設(shè)為document.body的高度,也并不一定能成功,因?yàn)閐ocument.body的高度可能沒有整個檔的高度大,如使用了層

正確寫法如下

1.var MainFrame = parent.document.getElementById("main");
2.MainFrame.style.height = document.documentElement.offsetHeight+"px";//最好在這上面還加上一個常數(shù),如10

通過iframe訪問主頁面

注:偽Ajax效果就是這樣模擬的了

01.
02. <script type="text/javascript">
03. window.onload = function() {//這是主頁面的代碼
04. var frame = document.getElementById("frame1");
05. var msg = frame.contentDocument.getElementById("message");
06. msg.innerHTML = "Hello World from Frame Page 1";
07. };
08. </script>
09.</pre>
10.<pre class="brush:javascript;gutter:false;toolbar:false">
11.//這是iframe部分!
12. <div class="errordisplay" id="message">
13.original
14.</div>
15.
16.<script type="text/javascript">
17. var msg = window.parent.document.getElementById("message");
18. msg.innerHTML = "Updated from iFrame";
19.</script>

document.getElementById取到的iframe是不能直接操作里面的document的,只能這樣?。?/p>

  • 在IE為是frames[id].document或document.getElementById(id).contentWindow.document;
  • 在firefox為frames[name].contentDocument或document.getElementById(id).contentDocument;

最后切記iframe在FF中是不綁定任何事件的!


Tag標(biāo)簽: iframe,元素,用法總結(jié)

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評論 (190人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號