按钮点击是否可以导致iframe中的事件?

假设我在Iframe旁边有一个按钮,并且iframe是一个下载链接。 当我点击外部按钮时,是否可以点击iframe上的下载链接?

http://jsfiddle.net/idude/9RQ3N/

  

例如,在上面的jsfiddle链接中,我如何才能通过单击按钮来下载w3图片?

提前致谢!

试试(这种模式)

 $(function () { var url = ["http://example.org"]; // `url`, eg, `http://example.org` var iframe = $(" 

jsfiddle http://jsfiddle.net/guest271314/2x4xz/

要访问iFrame DOM,您需要做的就是:

  var x = document.getElementById("myframe"); var y = x.contentWindow.document; 

或者你可以通过使用获得iFrame

  window.frames[0] 

或类似的东西,如果你不想使用iFrame的ID。

然后您可以在该文档中搜索某些内容并触发该元素上的单击事件。

  y.getElementById("somButton").click(); 

Mozilla的JSChannel设计用于使用POST与iframe进行通信。 你应该考虑检查一下。

公报的一个例子

父HTML可以是

    

现在为孩子:

  ...  

这个,在运行时,会产生并警告说你说“我是鲍勃!”

显然,你可以做很多事情。 玩得开心 :)

正如@ssergei正确指出的那样,出于安全原因这是不可能的。 这是我用来捕捉错误的jsFiddle: http : //jsfiddle.net/9RQ3N/5/ 。

这是Google Chrome引发的SecurityError消息:

 Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "http://www.w3schools.com". Protocols, domains, and ports must match. 

来自Firefox:

 Error: Permission denied to access property 'document' 

从Safari:

 Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "http://www.w3schools.com". Protocols, domains, and ports must match.