您好,欢迎来到点滴吧! 手机版
点滴吧www.diandiba.com
记录点点滴滴,尽在点滴吧
  • 文章
  • 文章
  • 特效
  • 素材
  • 景点
您现在的位置:首页 > JavaScript教程 > 文章正文
js操作iframe中的DOM元素
更新时间:2015/1/4 23:43:27   点击:2340次

操作iframe中的DOM元素,有两个注意点:

1、必须先获取指定iframe的document;

2、对于1,必须在页面load完以后才能获取;


举例说明:

<!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>操作iframe中的DOM元素</title> 
<script type="text/javascript"> 
<!-- 
//返回指定iframe的document 
function getIFrameDocument(aID) { 
    var rv = null; 
    if (document.getElementByIdx(aID).contentWindow.document){ 
        // if contentDocument exists, W3C compliant (Mozilla) 
        rv = document.getElementByIdx(aID).contentWindow.document; 
    } else { 
        // IE 
        rv = document.frames[aID].document; 
    } 
    return rv; 
} 
  
function bindEvents() { 
    var iDocument = getIFrameDocument('test'); 
    //接下来就可以进行类似的DOM操作了 
    var map = idocument.getElementByIdx('map'); 
    //…… 
} 
//--> 
</script> 
</head> 
  
<body onload="bindEvents()"> 
  
<iframe id="test" name="giscontent" src="iframe.html" frameborder="0" scrolling="no"> 
</iframe> 
  
</body> 
</html>


相关文章
导航分类
热门文章
关于我们| 联系我们| 免责声明| 网站地图|
CopyRight 2012-2015 www.diandiba.com - 点滴吧 All Rights Reserved
滇ICP备09005765号-2