简体   繁体   English

javascript - 在主页的 js 中获取 iframe 中 div 的内容

[英]javascript - get the content of a div inside an iframe in a js in the main page

Good day to all.祝大家有美好的一天。

I have this structure我有这个结构

  <html>

         <iframe>

                <div id="idd">Some Content</div>

         </iframe>       

         <script type="text/javascript">
                 function getIddContent()
                 {
                      //This is the part I ask about
                 }

                 var content = getIddContent();
         </script>
  </html>

I need to get the content of the div inside the content variable.我需要在 content 变量中获取 div 的内容。 Thank you for your time.感谢您的时间。 If I use document.getElementById("idd").innerHTML it says that the div doesn't exist.如果我使用 document.getElementById("idd").innerHTML 它说 div 不存在。

Use the following code使用以下代码

function getIddContent() {
    return window.frames[0].document.getElementById('idd').innerHTML;
}

But you probably have to include the iframe contents via the src attribute on the iframe.但是您可能必须通过 iframe 上的 src 属性包含 iframe 内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM