【问题标题】:What is the best way to set URL for <frame> in <frameset>在 <frameset> 中为 <frame> 设置 URL 的最佳方法是什么
【发布时间】:2016-06-16 04:01:58
【问题描述】:

我现在正在使用 framesetframe,我对如何更改框架 URL 感到很困惑。我的客户他们使用 locationsrc 来设置/更改框架 URL。有时 location 工作,有时 src 工作,所以我要找出发生了什么。
我创建了 3 个 html 文件:parent.htmlleft.htmlright.html。这是 parent.html 内容

<frameset cols="*,*">
   <frame id="left" src="left.html">
   <frame id="right" src="right.html">
</frameset>

这是left.html内容

<head>
    <script>
        function LocationThis() {
            window.frames['innerleft'].location = "http://prntscr.com";
        }
        function SrcThis() {
            window.frames['innerleft'].src = "http://prntscr.com";
        }
        function LocationOther() {
            window.parent.frames['right'].location = "http://prntscr.com";
        }
        function SrcOther() {
            window.parent.frames['right'].src = "http://prntscr.com";
        }
    </script>
</head>
<body>
    <h1>Left</h1>
    <button type="button" onclick="LocationThis();">Location this</button>
    <button type="button" onclick="SrcThis();">Src this</button>
    <button type="button" onclick="LocationOther();">Location other</button>
    <button type="button" onclick="SrcOther();">Src other</button>
    <hr/>
    <iframe id="innerleft" src="" />
</body>

只有 src 在这种情况下有效。那么为什么 location 有时会起作用,而 src 不会呢?好的,现在我像这样更改脚本

//window.parent.frames['right'].location = "http://prntscr.com";
window.parent.frames[1].location = "http://prntscr.com";

然后 位置 工作正常。所以 frames['right'].srcframes[1].location 然后。但并非所有情况,有时 frames['right'].location 也可以。当我删除 id 并仅使用框架名称时,我必须使用 location

<frame name="right" src="right.html">

就是这样。哦,感谢 IE 拒绝接受 src,所以我必须像这样检查

try {
  window.parent.frames["id"].src = {url};
}
catch(er) {
  window.parent.frames["id"].location = {url};
}

对于这整个事情有一个巧妙的解决方案吗?

【问题讨论】:

标签: javascript html frame frameset


【解决方案1】:

我实际上无法确认这一点,因为在 Chrome 上它甚至不起作用(查看 window.frames 对象上的 de docs),但我假设通过 ID 获取帧将返回对HTML 元素,它具有 src 属性 - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frame

当您正确执行此操作(按索引获取)时,您将获得对该框架的 window 对象的引用,该对象具有 location 属性 - https://developer.mozilla.org/en-US/docs/Web/API/Window

所以,我的猜测是 window.frames['id'].src 在某些情况下可以工作,因为浏览器实现有些古怪,但实际上您的选择是:

  1. window.frames[index].location
  2. document.getElementById('FrameID').src

【讨论】:

  • 可能你使用 file:/// 运行。 window.frames[index].location 变得一团糟,只有 3-4 帧。按 ID 获取是最好的选择,除非它们不能在 IE
猜你喜欢
  • 1970-01-01
  • 2014-07-30
  • 2020-08-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2012-09-21
相关资源
最近更新 更多