【问题标题】:Iframe inside Iframe cant do changesiframe 里面的 iframe 不能做改变
【发布时间】:2013-02-04 18:35:11
【问题描述】:

我在 iframe 上有一个 iframe,我想用这段代码更改图像的 src

window.top.frames[0].frames[0].document.getElementById("maquina").src = "https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png";

当我尝试它时,它可以工作,但我看不到页面中的变化,只有当 e 读取 .src 时才会出现在代码中 那个元素的

【问题讨论】:

    标签: javascript


    【解决方案1】:

    如果没有访问子 DOM 的全局方法,您将无法在父框架内操作 iframe 的内容。有一个答案Here可以满足你的需要,但我会复制下面以供参考:

    在子 iframe 中:

    parent.childGetElementById = function (id) {return document.getElementById(id);}
    parent.childLoaded();
    

    在父级中:

    function childLoaded() {var dom = childGetElementById('someid');}
    

    假设您实际上可以访问子页面,我建议您修改您的设计,避免使用 iframe。

    [回复评论]

    这取决于您是直接使用 HTML 还是脚本语言(如 PHP)。如果是纯 HTML,您可以使用服务器端包含

    <!-- #include virtual="path/to/included/file.html" -->
    

    在PHP中,你可以像这样包含页面

    require('/real/path/to/included/file.php');
    

    不要误会我的意思,iFrame 在某些特定情况下很有用(规避跨域源策略、oAuth 实施等);它们经常被滥用而不是正确使用。

    【讨论】:

    • 好的,我会尝试的,但是在没有 iframe 的情况下创建 html“模块”的最佳方法是什么?
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多