2014-10-10 68 views
0

需要通过父窗口的iframe元素中的简单JavaScript触发点击事件。父窗口iframe元素触发器通过简单JavaScript点击

父窗口

<html> 
    <head> 
    <meta http-equiv="Content-Language" content="en" /> 
    <title>Main window</title> 
    </head> 
    <body> 
     <iframe scrolling="auto" src="html_test.php"></iframe> 
     <button onclick='window.open("child.html")'>Open window</button> 
    </body> 
</html> 

子窗口

<script type="text/javascript"> 
    window.opener.document.getElementsByTagName('iframe')[0].document.getElementById('main_tab').click(); 
</script> 

但onload事件的子窗口得到这个JavaScript错误:

TypeError: window.opener.document.getElementsByTagName('iframe')[0].document is undefined 

以及文件html_test.php div元素main_tab在那里有效。

html_test.php HTML是:

<html> 
    <head> 
    <title>test iframe</title> 
    </head> 
    <body> 
     <ul> 
      <li id="main_tab" onclick="alert('test click')"></li> 
     </ul> 
    </body> 
</html> 
+0

window.opener.document.getElementsByTagName(...)[0] .'contentDocument' – 2014-10-10 11:23:56

+0

thnks @gp我得到这个错误:::: TypeError:window.opener.document.getElementsByTagName('iframe')[0] .document is undefined' – 2014-10-10 11:26:58

+0

文档对iframe元素无效。改用contentDocument。 – 2014-10-10 11:29:42

回答

0

你的首战窗口形式ifream页,所以简单的做到这些代码

window.opener.document.getElementById('main_tab').click(); 
+0

是的,完成!!!!!!谢谢 !!! :) – 2014-10-15 05:41:27

0

你的二级页面(即加载到iframe)是错误的。它至少需要一个<meta charset="UTF-8" />,并且可能需要顶部的DOCTYPE声明。另外,在我的代码实验中(在FireFox中),我遇到了window.opener问题,需要使用window.parent代替。您一定需要使用contentDocument而不是document。对我而言,在进行这些更改后,错误消失了。

相关问题