如何捕获iframe中的onkeydown、onkeyup、onclick事件
				
									
					
					
						|  | 
							admin 2011年1月22日 21:33
								本文热度 7580 | 
					
				 
				要求可以實現, 不過如果要跨域訪問其他網頁就要解決跨域訪問的權限問題,下面給出非跨域情況的1.html的代碼供參考 
這個假設為包含iframe的主頁面1.html的代碼, 2.html的代碼就請大家隨便添加好了 
- HTML code
 <!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>
 <title>未命名頁面</title>
 
 <script type="text/javascript" language="javascript">
 <!--
 
 var subWindow = null;//用來保存iframe中的引用頁面的window對象
 
 function Show()
 {
 if (subWindow == null)
 {
 //alert(event.srcElement.tagName);
 var iframe = document.getElementById('iframe');//取得iframe
 subWindow = iframe.contentWindow;//獲取iframe引用頁面的window對象並保存起來
 subWindow.document.onclick = GetInitiator;//給iframe引用頁面的document對象定義onclick事件(其他的事件的定義與此相似)
 }
 }
 
 function GetInitiator()
 {
 alert(subWindow.event.srcElement.tagName);//subWindow.event用來獲取iframe引用頁面的event對象,以便後面處理用
 }
 
 //-->
 </script>
 
 </head>
 <body onload="Show()">
 <input type="button" id="btn1" value="Button 1" />
 <input type="checkbox" id="cb1" value="cb1" />
 
 <iframe src="2.html" id="iframe">
 </iframe>
 
 </body>
 </html>
 
 
 
该文章在 2011/1/22 21:33:12 编辑过