您现在的位置: 365建站网 > 365学习 > 总结iframe的使用第一部分

总结iframe的使用第一部分

文章来源:365jz.com     点击数:1346    更新时间:2009-09-20 08:53   参与评论
可能大家对iframe标签并不陌生吧,可是否能很正确地使用它呢?
 1.  js怎么使用iframe呢?
    ifr是一个以存在的 iframe 的 ID 和 NAME 值:
      document.getElementById(“ifr”);
      window.frames[“ifr”];
    这两个对象有什么区别呢?
    要想使用iframe内的函数,变量就必须通过window.frames[“ifr”].因为它取的是一个完整的DOM模型
    如果只想改变iframe的 src 或者width,scrolling 等属性就要用document.getElementById(“ifr”);
    还要注意的是,如果在iframe的页面未完全装入的时候,调用iframe的DOM模型,会发生很严重的错误,所以,你要准备一个容错模式.

举例说明:
  在网页iframe1.HTML中代码如下
  <iframe id="eyejsF"   frameborder="1"  width="100px"  src="eyejs.html"></iframe>
<br/>
<input type="button" value="获取或设置iframe内的信息方式一" onclick="opIframeDom()"/>
<input type="button" value="获取或设置iframe内的信息方式二" onclick="opIframeDom1()"/>
 <input type="button" value="获取或设置iframe内的信息方式三" onclick="opIframeDom2()"/>
<input type="button" value="获取或设置本身的属性" onclick="opIframeAttribute()"/>

<script language="JavaScript">
//案例演示,
利用document.getElementById("ifr").contentWindow来获取src中包含网页(就叫B网页)的DOM对象,
document.getElementById("ifr").contentWindow就相当于B网页的window对象

 在IE为是
window.frames[ id ]document.getElementById(id).contentWindow
 在firefox为window.frames[ name ]document.getElementById(id).contentWindow
注意啦
IE下是window.frames[ id ]  而FF是window.frames[ name ]  也就是iframe标签必需有id和name再能用此方法
请看下面四种方法(已在IE 6 7 8和FF下测试过)
/**/
var opIframeDom=function(){
    var ifrObj = window.document.getElementById('eyejsF');
    var ifrDom=ifrObj.contentWindow; //在IE 6 7 8和FF下 都通过,因此为了兼容IE和FF就用此方法
    alert(ifrDom.document.getElementById("eyejsID").innerHTML);
    ifrDom.document.getElementById("eyejsID").innerHTML="我现在在eyejs网上";
};
var opIframeDom1=function(){
     var ifrDom = window.frames['eyejsF'];  //在FF下iframe中必需有name再能有效
    alert(ifrDom.document.getElementById("eyejsID").innerHTML);
    ifrDom.document.getElementById("eyejsID").innerHTML="我现在在eyejs网上";
};
var opIframeDom2=function(){
    var ifrObj = window.document.getElementById('eyejsF');
    var ifrDom=ifrObj.contentDocument;//在IE 6和IE 7中是undefined,而在IE 8和FF中可以用
    alert(ifrDom)
    alert(ifrDom.getElementById("eyejsID").innerHTML);
    ifrDom.getElementById("eyejsID").innerHTML="我现在在eyejs网上";
};
/*本身的属性*/
var opIframeAttribute=function(){
   var ifrObj = window.document.getElementById('eyejsF');
   alert(ifrObj.width);
   ifrObj.width="200px";
}
</script>
-------------------------------------------------------------------------------------------------------------------------------------------
iframe的包含网页eyejs.html
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html XMLns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<div id="eyejsID">我在eyejs.html</div>
</body>
</html>
小结如下:
1.获取iframe对象 可以用document.getElementById('eyejsF')和window.frames['eyejsF']
      但是用window.frames['eyejsF']此方法时, 在FF下iframe中必需有name再能有效
2.获取iframe对象的包含网页的Dom对象就要用 iframe对象.contentWindow或iframe对象.contentDocument
    但是iframe对象.contentDocument在IE 6 和IE 7中不支持,IE 8又支持了
因此为了兼容IE和FF,获取iframe中的dom对象建议用第一种方法document.getElementById('ifr').contentWindow.document.getElementById(id)对象来操作
了解了iframe的使用后,就可以参考下面的属性来做一些东西了
-------------------------------------------------------------------------------------------------------------------------------------------
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

-------------------------------------------------------------------------------------------------------------------------------------------
还要注意一些特殊情况:
1. 通常要隐藏iframe的边框时需要用到frameborder属性
2.    var iframe = document.createElement('iframe');
       iframe.setAttribute('frameborder',0);//Firefox下有效,IE下无效
       而如果我们需要直接对其属性进行赋值:
       iframe.frameBorder=0;//Firefox和IE均有效 为了兼容IE和FF,就用此方法
3. 如果想要为浮动框架定义透明内容,则必须满足下列条件:
    * 与 iframe 元素一起使用的 allowTransparency 标签属性必须设置为 true。
    * 在 iframe 内容源文档,background-color 或 body 元素的 bgColor 标签属性必须设置为 transparent。
4. 去掉动态创建的iframe的边框
       var iframe = document.createElement('iframe');
       iframe.scrolling="no";//这两个属性的设置缺一不可。
      iframe.frameborder="no";
5.有关高度自适应问题
   scrollHeight返回的是一个数字,没有带单位
  在IE 下如果没有DTD声明,则按loose.dtd 解析,在设置对像的宽高时,会自动的加上单位"px"
  将iframe的高度设为document.body的高度,也并不一定能成功,因为document.body的高度可能没有整个档的高度大,如使用了层
  正确写法如下
       var MainFrame = parent.document.getElementById("main");
       MainFrame.style.height = document.documentElement.offsetHeight+"px";//最好在这上面还加上一个常数,如10
案例下载:    http://www.eyejs.com/html/79/n-179.html

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (1346人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------