【问题标题】:Getting the img tag inside iframe在 iframe 中获取 img 标签
【发布时间】:2010-10-04 05:07:55
【问题描述】:

我有一个类名="blaclass" 的 iframe。 在其中,我有一个没有名称(或 id)的 div,在这个 id 中有一个类名 =“classs”的 div。在这个 div 中,有两张图片(没有类名/id)。

我怎样才能得到那些 image.src?如果有原型版本就更好了!

PS:我无法在页面完全呈现之前添加类名/ID(我没有创建 iframe - 它是通过 javascript 动态创建的,然后我运行我的脚本)。

谢谢

【问题讨论】:

    标签: javascript iframe prototype


    【解决方案1】:

    a.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <body onLoad="foo()">
      <iframe src="b.html">
        </iframe>
    <script type="text/javascript">
    function foo()
    {
    alert(window.frames[0].document.getElementsByClassName("classs")[0].getElementsByTagName("img")[0].src);
    }
    </script>
    </body>
    </html>
    

    b.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <body>
       <div>
       <div class="classs">
       <img id="img" src="test.png" />
       <img id="img" src="test.png" />
       </div>
       </div>
    </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      还要确保 iframe 所在的域与其指向的域相同。否则您将无法从 iframe 中获取任何信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-13
        • 2013-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多