现在的位置: 首页 > 综合 > 正文

小知识点:iframe 自页面操作父页面,firefox监听事件

2013年09月12日 ⁄ 综合 ⁄ 共 5409字 ⁄ 字号 评论关闭

一般javascript无法控制页面内部的iframe,而很多情况下,需要在父页面数据变换时同步更新iframe里的内容. 此时可利用javascript的机制,在iframe内部写父页面的js事件.比如,父页面有一TextBox,ID为txtText,当TextBox的值变换时,子页面需要显示这个值.此时可在iframe 页面内输入: parent.document.getElementByID("txtTest").onchange = function() { document.body.innerHTML = parent.document.getElemntByID("txtTest").value; }

 

  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. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
  5. <title>attachEvent Test</title>   
  6. <style type="text/css">   
  7. html,body   
  8. {   
  9.     padding:0px;margin:0px;   
  10.     font-family: "宋体";color: #4b4b4;background-color: #fff;   
  11.     height:100%;text-align: center;   
  12. }   
  13. </style>   
  14.  </head>   
  15.  <body>   
  16.     <br />   
  17.     <div style="width:100%;height:200px;border:1px solid #ccc;">   
  18.         <div id="top_mycoomix_div" style="display:block;border:1px solid #ccc;">   
  19.             attachEvent test   
  20.         </div>   
  21.     </div>   
  22. <script type="text/javascript">   
  23. //方法一   
  24. Object.prototype.attachEvent = function(method,func)   
  25. {   
  26.     //alert(this);//[object HTMLBodyElement]   
  27.     if(!this[method]){   
  28.         this[method]=func;   
  29.     }   
  30.     else{   
  31.         this[method]=this[method].attach(func);   
  32.     }   
  33. }   
  34. Function.prototype.attach=function(func){   
  35.     var f=this;   
  36.     return function(){   
  37.         f();   
  38.         func();   
  39.     }   
  40. }   
  41.   
  42. function hiddenTopMycoomix()   
  43. {   
  44.     alert("into hiddenTopMycoomix function");   
  45.     document.getElementById("top_mycoomix_div").style.display = "none";   
  46. }   
  47.   
  48. //监听document.body必须设置body的样式height:100%;点击页面才能达到预期效果,否则只能在可见区域点击才有效.   
  49. //监听document则不存在这个问题   
  50. //不标准的调用方式   
  51. //window.document.body.attachEvent("onclick",function(){hiddenTopMycoomix();});   
  52. //window.document.body.attachEvent("onclick",hiddenTopMycoomix);   
  53. </script>   
  54.   
  55. <script type="text/javascript">   
  56. //方法二   
  57. function addListener(element,e,fn){ element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn)};   
  58. function removeListener(element,e,fn){ element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn)};   
  59. //封装后统一的调用方式   
  60. //addListener(document.body,"click",hiddenTopMycoomix);   
  61. </script>   
  62.   
  63. <script type="text/javascript">   
  64. //方法三   
  65. //引入用到的coos脚本库部分,实际应用中不在页面上而采用导入js文件的方式   
  66. var coos = function(){this.version = "1.0";};   
  67.   
  68. coos.$id = function(id)   
  69. {   
  70.   return document.getElementById(id);   
  71. };   
  72.   
  73. coos.$obj = function(el)   
  74. {   
  75.     var obj = el;   
  76.     if(typeof(el) == "string")   
  77.     {   
  78.         obj = document.getElementById(el);   
  79.     }   
  80.     return obj;   
  81. };   
  82.   
  83. coos.event = function(e)   
  84. {   
  85.     var e = e || window.event;   
  86.     return e;   
  87. };   
  88.   
  89. coos.event.format = function(e)    
  90. {   
  91.     var e = e || window.event;   
  92.     try  
  93.     {   
  94.         if(!e.pageX && e.clientX)//firefox3 nonsupport pageX   
  95.         {   
  96.             e.pageX = e.clientX + document.body.scrollLeft;   
  97.             e.pageY = e.clientY + document.documentElement.scrollTop;   
  98.         }   
  99.     }   
  100.     catch(e){}   
  101.        
  102.     if(window.event)   
  103.     {   
  104.         e.charCode = (e.type == "keypress") ? e.keyCode : 0;   
  105.         e.eventPhase = 2;   
  106.         e.isChar = (e.charCode > 0);   
  107.         e.preventDefault = function ()   
  108.         {   
  109.             this.returnValue = false;   
  110.         };   
  111.            
  112.         if(e.type == "mouseout")   
  113.         {   
  114.             e.relatedTarget = e.toElement;   
  115.         }   
  116.         else if(e.type == "mouseover")   
  117.         {   
  118.             e.relatedTarget = e.formElement;   
  119.         }   
  120.            
  121.         e.stopPropagation = function ()   
  122.         {   
  123.             this.cancelBubble = true;   
  124.         };   
  125.            
  126.         e.target = e.srcElement;   
  127.         e.time   = (new Date()).getTime();   
  128.     }   
  129.        
  130.     try  
  131.     {   
  132.         if (e.target.nodeType == 3)   
  133.         {// defeat Safari bug   
  134.             e.target = e.target.parentNode;   
  135.         }   
  136.         //如果不存在keyCode属性,同时存在which属性则赋值,因为该属性为只读属性(has only a getter)   
  137.         if(!e.keyCode && e.which)   
  138.         {   
  139.             e.keyCode = e.which;   
  140.         }   
  141.     }   
  142.     catch(e){}   
  143.        
  144.     return e;   
  145. };   
  146.   
  147. coos.event.add = function(el,EventType,callback)   
  148. {   
  149.     var obj = coos.$obj(el);   
  150.        
  151.     if(obj.addEventListener)   
  152.     {   
  153.         obj.addEventListener(EventType,callback,false);   
  154.     }   
  155.     else if(obj.attachEvent)   
  156.     {   
  157.         obj.attachEvent('on'+EventType,callback);   
  158.     }    
  159.     else  
  160.     {   
  161.         obj['on'+EventType] = callback;   
  162.     }   
  163. };   
  164. //方便页面加载完成后执行方法的接口   
  165. coos.onloadEvent = function(fn)   
  166. {   
  167.     if (window.addEventListener)    
  168.     {   
  169.         window.addEventListener("load",fn,false);   
  170.     }   
  171.     else if(window.attachEvent)   
  172.     {   
  173.         window.attachEvent("onload", fn);   
  174.     }   
  175.     else  
  176.     {   
  177.         window.onload = fn;   
  178.     }   
  179. };   
  180.   
  181. //以下为实现代码部分   
  182. var coomixTimeout = null;   
  183. function hiddenTopMycoomix2(e)   
  184. {   
  185.     var obj = coos.$id("top_mycoomix_div");   
  186.     if(!obj){return;}   
  187.     var e = coos.event.format(e);   
  188.     coomixTimeout = setTimeout(function(){obj.style.display = "none";},100);   
  189.     if(e.target == obj)   
  190.     {   
  191.         clearTimeout(coomixTimeout);   
  192.     }   
  193. }   
  194. coos.event.add(document,"click",hiddenTopMycoomix2);   
  195.   
  196. //在DOM元素加载完成前的实现代码,需要在页面元素加载完成后执行,一般情况下在window.onload后执行   
  197. coos.onloadEvent(function(){   
  198.     var obj = coos.$id("testDom");   
  199.     obj.innerHTML = obj.innerHTML + "<br />after set value";   
  200.     setTimeout(function(){obj.style.display = "none";},500);   
  201. });   
  202.   
  203. </script>   
  204. <div id="testDom">testDom</div>   
  205.  </body>   
  206. </html>  

 

抱歉!评论已关闭.