文档不会加载到名称已更改但正确的 iframe 中



我试图实现的是能够通过Java更改它们的名称,将href的内容加载到三个iframe之一中。

问题是:Chrome 总是将内容加载到第一个 iframe 中,忽略它的名称更改,这个问题在 Firefox 中不会出现。

function change_name()
{
document.getElementById('frame1').name = "#" ;
document.getElementById('frame2').name = "main_frame" ;
};
.frame {
    width:100px;
    height:100px;
}
<iframe id="frame1" name="main_frame" class="frame"></iframe>
<iframe id="frame2" name="#" class="frame"></iframe>
<iframe id="frame3" name="#" class="frame"></iframe>
<input type="submit" onclick="change_name()" value="change iframe name"/>
<a href="www.stackoverflow.com" target="main_frame">load page into iframe</a>

jsfiddle 演示了我正在尝试做的事情:http://jsfiddle.net/514y7v4m/1/
任何帮助都非常感谢

偶然发现了这个链接:http://help.dottoro.com/ljbimuon.php

在您的脚本进行一些修改后,我相信这可能是解决方案。更改 contenWindow.name 而不是单独更改名称似乎可以解决问题。

function change_name() {
    var one = document.getElementById('frame1');
    one.contentWindow.name = "#";
    console.log("The name is: " + one.name);
    console.log("The name of the window: " + one.contentWindow.name);
    var two = document.getElementById('frame2');
    two.contentWindow.name = "main_frame";
    console.log("The name is: " + two.name);
    console.log("The name of the window: " + two.contentWindow.name);
};

最新更新