【问题标题】:Links on iframe creates another view of page in IFrameiframe 上的链接在 IFrame 中创建另一个页面视图
【发布时间】:2013-06-07 07:06:53
【问题描述】:

我创建了一个 html 并将其用作 iframe 的来源。

在 iframe 中,我在 div 上添加了一个链接(参见下面的示例代码)

<div onclick="location.href='/cybersecurity/risk-management/index.cfm'; location.href='_parent';" >
    <b>Sample Text</b><br />
    <span>
        Sample Text
    </span>
</div>

当我试图点击 iframe 页面内的 div 时。它在 iframe 内创建页面的另一个视图。但是对于 iframe 中的锚点工作正常,&lt;a href="linkhere" target="_parent" &gt;This is an anchor!&lt;/a&gt;。如何在我的 div 的 onClick 添加 JavaScript?

我也尝试使用&lt;base target="_parent" /&gt;,但它不起作用。

【问题讨论】:

    标签: javascript html iframe target


    【解决方案1】:

    您希望设置最顶层窗口(父窗口)的位置,而不是设置window.location.href,即当前窗口(iframe)。

    window.top.location.href = 'foobar'
    

    您也可以使用window.parent,它将使用当前窗口的直接父窗口,但在大多数情况下,使用window.top 更安全,因为您实际上想要最顶层的父窗口。

    【讨论】:

    • 谢谢! :D 我从不后悔在这里解释我的问题。哈哈哈
    【解决方案2】:
    <div onclick="parent.document.location.href='/cybersecurity/risk-management/index.cfm';" >
        <b>Sample Text</b><br />
        <span>
            Sample Text
        </span>
    </div>
    

    这会触发您对更改 url 的父视图。

    https://developer.mozilla.org/en-US/docs/Web/API/window.parent

    【讨论】:

    • 这件事也有效,但我必须感谢@Jamie Dixon,他首先回答了我的问题。不过谢谢!为你 +1 点赞!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2013-12-04
    • 2011-05-08
    • 1970-01-01
    相关资源
    最近更新 更多