javascript - 访问 iframe 内的函数

标签 javascript html iframe

我知道这已被多次提及,但我找不到我要找的确切内容。

Index.html 代码。

<html>
 <head>
    <meta charset="utf-8">
    <title>myPage</title>
    <script src="libs/h5.js"></script>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 </head>
 <body>
   <div id="content" width="1025" height="670">
      <iframe id="homePage" src="pages/homepage.html" width="1025" height="670" seamless></iframe>
   </div>
 </body>
</html>

h5.js代码

function setTextHidden()
{
       <------------ **How do I call the hideText function inside the iframe from the parent?**
}

iframe 中 homepage.html 的 js 代码

function hideText()
{
   console.log("hideText");
}

最佳答案

您可以使用window.frames属性来访问 iFrame 的 window 对象,您可以从中调用 hideText:

function setTextHidden() {
    window.frames[0].hideText(); // assuming this is the first iframe on your page
}

关于javascript - 访问 iframe 内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16181981/

相关文章:

css - IFRAME:在 IE 和 Edge 中悬停无法按预期工作

javascript - Angular - 自定义结构指令 - ngForIn : How can I create my own local variable?

javascript - 我们可以在html5中为canvas中的形状分配类吗

html - 填充文本填充溢出不影响分词

html - 4 象限 div,中间有 logo div

html - 使用 iframe 显示静态文本内容

javascript - jstree在非根节点使用refresh_node时出现的问题

javascript - 可汗学院算法 : Binary Search Solution

html - 以百分比和 em/px 指定的不同尺寸行为

html - 带有 iframe 的静态左列填充页面右侧的剩余部分