调用iframe中定义的Javascript函数[重复]

时间:2021-10-10 23:57:11

This question already has an answer here:

这个问题在这里已有答案:

I want to call javascript function (on click()) which is inside Iframe

我想调用iframe里面的javascript函数(在click()上)

<a onclick="abc();" href=# >Call Function which is inside Iframe </a>

<iframe id=myFrame>
   <script>
     function abc(){
         alert("I am inside Iframe");
     }
    </script>
</iframe>

It will be better if there is solution in JQuery.

如果JQuery中有解决方案会更好。

Thank you

3 个解决方案

#1


30  

You can do that this way:

你可以这样做:

<a onclick="$('#myFrame')[0].contentWindow.abc();">Call function which is inside of Iframe</a>

Basically, I get a reference to the Iframe. contentWindow is the global (window) object for the iframe, and all global functions in a document are methods of window.

基本上,我得到了对iframe的引用。 contentWindow是iframe的全局(窗口)对象,文档中的所有全局函数都是窗口的方法。

As a note, you can only do this if both pages, the one containing the iframe and the one in the iframe come from the same domain.

请注意,只有两个页面(包含iframe的页面和iframe中的页面来自同一个域)都可以执行此操作。

#2


21  

Use this to access your frame function:

使用它来访问您的框架功能:

    document.getElementById('myFrame').contentWindow.abc()

#3


2  

You could also give the iframe a name='frameName', and then reference from the parent like this:
onclick="window.frameName.abc()"

你也可以给iframe一个name ='frameName',然后像这样从父节点引用:onclick =“window.frameName.abc()”

#1


30  

You can do that this way:

你可以这样做:

<a onclick="$('#myFrame')[0].contentWindow.abc();">Call function which is inside of Iframe</a>

Basically, I get a reference to the Iframe. contentWindow is the global (window) object for the iframe, and all global functions in a document are methods of window.

基本上,我得到了对iframe的引用。 contentWindow是iframe的全局(窗口)对象,文档中的所有全局函数都是窗口的方法。

As a note, you can only do this if both pages, the one containing the iframe and the one in the iframe come from the same domain.

请注意,只有两个页面(包含iframe的页面和iframe中的页面来自同一个域)都可以执行此操作。

#2


21  

Use this to access your frame function:

使用它来访问您的框架功能:

    document.getElementById('myFrame').contentWindow.abc()

#3


2  

You could also give the iframe a name='frameName', and then reference from the parent like this:
onclick="window.frameName.abc()"

你也可以给iframe一个name ='frameName',然后像这样从父节点引用:onclick =“window.frameName.abc()”