【问题标题】:Getting an iframe and then access it as a document获取 iframe,然后将其作为文档访问
【发布时间】:2014-02-26 07:51:08
【问题描述】:

我在访问 iframe 以操作框架内的内容时遇到问题。

<iframe id="verify" src="http://www.wesite.com/"></iframe>

当我想用js访问它时,它告诉我它是未定义的

var frame = document.getElementById("verify");

如果这样可行,那么我将如何访问该框架中的内容来操作它?会不会是:

frame.document.getElementById("ElementInsideIframe").InnerHtml="etc";

【问题讨论】:

    标签: javascript iframe


    【解决方案1】:

    这将工作检查它:

      if ( frame.contentDocument ) {
        doc = objHTML.contentDocument;
      } else {
        doc = objHTML.contentWindow.document;
      }    
       doc .getElementById("ElementInsideIframe").InnerHtml="etc";
    

    注意:对于跨域这种 JS 是行不通的...

    【讨论】:

      【解决方案2】:

      你可以如下使用:

      var temp = document.getElementsByTagName("iframe");
       var innerDoc = temp[0].contentDocument || temp[0].contentWindow.document;
      
       innerDoc.getElementById("ElementInsideIframe").innerHTML="Your Text";
      

      【讨论】:

        猜你喜欢
        • 2017-03-17
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 2020-09-15
        • 1970-01-01
        • 2013-10-28
        相关资源
        最近更新 更多