0

我创建了一个 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 中的锚点工作正常,<a href="linkhere" target="_parent" >This is an anchor!</a>. 如何在onClick我的 div 上添加 JavaScript?

我也尝试使用<base target="_parent" />,但它不起作用。

4

2 回答 2

1

与其设置window.location.href当前窗口(iframe),不如设置最顶层窗口(父窗口)的位置。

window.top.location.href = 'foobar'

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

于 2013-06-07T07:13:27.740 回答
1
<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

于 2013-06-07T07:13:37.900 回答