【问题标题】:Dynamic Iframe IE Name Issue动态 iframe IE 名称问题
【发布时间】:2011-01-09 11:04:44
【问题描述】:

这似乎是一个常见问题,我看到的解决方案似乎对我不起作用。

我们有一个动态生成和插入的 iframe,以及一个提交给它的表单。在 FF 和 IE8 中运行良好,但在 7 中我们遇到了 iframe 名称问题(IE 不想使用 element.name 或 element.setAttribute("name",) 设置 iframe 名称)。

所以我已经将我们的代码转换为:

函数 insertTrafRepiFrame(){ var contDiv = document.getElementById('trafficReportDiv'); if(contDiv != null){ 变量 iFrame; 尝试{ iFrame = document.createElement('IFRAME'); }抓住(前){ iFrame = document.createElement('

【问题讨论】:

    标签: javascript iframe


    【解决方案1】:

    我认为您使用document.createElement('<iframe name="trafficreport">'); 做得对

    但我不确定它是否被调用,因为 document.createElement('IFRAME') 可以,即使在 IE 中也是如此。

    我们使用动态 iframe 来沙箱跨域 JSONP 调用,它在 IE 中运行良好。 我使用的代码是:

    var ifr = (/MSIE (6|7|8)/).test(navigator.userAgent) ? 
        document.createElement('<iframe name="'+id+'">'):
        document.createElement('iframe');
        ifr.name = id;
    

    您可以点击演示链接查看working example here。如果演示有效,则表明您的代码有问题,如果它不起作用,则说明您的 IE 配置有问题。

    【讨论】:

    • createElement('IFRAME') 不是问题,因为那是非 ie 位,使用该方法在 ff 中可以正常工作。
    • 对不起,我不明白什么是不工作的。对我来说,代码中的 catch 子句永远不会被调用,因为即使在 IE7 中,try 子句也会成功。我记得让上面链接中的示例在 IE 中工作的痛苦时刻,尤其是使用 name 属性。可能是如果你放了一个调试器(Visual Web Express 可以远程运行)。您会看到这些步骤,并且可能会发现不同之处。
    • 啊,对不起,是的。我误解了你想说的话。我已经在 try catch 中切换了两个语句,现在开始测试。
    【解决方案2】:

    唯一对我有用的解决方案是在元素被注入 DOM 后更改名称:

    window.frames[name].name=name;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 2010-10-07
      • 1970-01-01
      • 2014-10-12
      相关资源
      最近更新 更多