【问题标题】:Getting parent value in an iframe? [duplicate]在 iframe 中获取父值? [复制]
【发布时间】:2013-08-05 14:41:45
【问题描述】:

我有以下两个 html 文件。在PassValueIFrame.html 中,我有一个引用inputForm.html 的iframe。另外,我在PassValueIFrame.html 中有一个隐藏字段,我试图在inputForm.html 中检索它的值,但没有得到它的值,它警告为'undefined'。我在这里做错了吗?请帮帮我。

PassValueIFrame.html

<html>
  <head>
  <title>IFrame Example</title>
  </head>
<body>
<input type="hidden" class="Language" value="English">
<iframe name="iframe" id="iframe_id" src="inputForm.html" height="150" >
</iframe>
</body>
</html>

inputForm.html

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script language="javascript">
  $(document).ready(function() {
    alert($(this).parent().find('.Language').html());
  });

  </script>
   <title>IFrame Child Example</title>
</head>
<body>
<h1> Iframeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee </h1>
</body>

谢谢!

【问题讨论】:

标签: javascript jquery html


【解决方案1】:
<input id="myInput" type="hidden" class="Language" value="English">

$("#myInput", window.parent.document).val();

【讨论】:

    【解决方案2】:

    试试这个:

    alert(parent.document.getElementsByClassName("Language")[0].value);
    

    或将id(例如languageId)添加到隐藏元素并尝试

    alert(parent.document.getElementById("languageId").value);
    

    【讨论】:

      【解决方案3】:

      您可以做的另一件事是在父窗口中定义一个函数并从 iframe 中调用它:

      inputForm.html

      <html>
      <head>
          <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
          <script language="javascript">
              $(document).ready(function() {
                  window.parent.parent_function("Papa!");
              });
          </script>
          <title>IFrame Child Example</title>
      </head>
      <body>
          <h1> Iframeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee </h1>
      </body>
      </html>
      

      PassValueIFrame.html

      <html>
          <head>
          <title>IFrame Example</title>
          <script>
              function parent_function(str) {
                  window.alert(str);
              }
          </script>
      </head>
      <body>
          <input type="hidden" class="Language" value="English">
          <iframe name="iframe" id="iframe_id" src="inputForm.html" height="150" >
          </iframe>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2011-04-13
        • 2017-08-08
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 2016-06-30
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多