操作iframe中的DOM元素

操作iframe中的DOM元素,有两个注意点:
1、必须先获取指定iframe的document;
2、对于1,必须在页面load完以后才能获取;

举例说明:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <title>操作iframe中的DOM元素</title>
  7. <script type="text/javascript">
  8. <!--
  9. //返回指定iframe的document
  10. function getIFrameDocument(aID) {
  11.     var rv = null;
  12.     if (document.getElementById(aID).contentWindow.document){
  13.         // if contentDocument exists, W3C compliant (Mozilla)
  14.         rv = document.getElementById(aID).contentWindow.document;
  15.     } else {
  16.         // IE
  17.         rv = document.frames[aID].document;
  18.     }
  19.     return rv;
  20. }
  21.  
  22. function bindEvents() {
  23.     var iDocument = getIFrameDocument('test');
  24.     //接下来就可以进行类似的DOM操作了
  25.     var map = iDocument.getElementById('map');
  26.     //……
  27. }
  28. //-->
  29. </script>
  30. </head>
  31.  
  32. <body onload="bindEvents()">
  33.  
  34. <iframe id="test" name="giscontent" src="iframe.html" frameborder="0" scrolling="no">
  35. </iframe>
  36.  
  37. </body>
  38. </html>
17fav 收藏本文
标签:, , ,

相关日志 随机文章

Comments

Leave a Reply




请输入验证码

Use "<coolcode></coolcode>" to publish your code.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

京ICP备05059555号

收藏 & 分享

Powered by 17fav.com