我创建了一个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>
。如何在我的div的onClick
上添加JavaScript?
我也尝试使用<base target="_parent" />
,但它不起作用。
答案 0 :(得分:1)
您需要设置最顶层窗口(父级)的位置,而不是设置window.location.href
,即当前窗口(iframe)。
window.top.location.href = 'foobar'
您还可以使用window.parent
,它将使用当前父窗口的当前父窗口,但在大多数情况下,使用window.top
更安全,因为您实际上想要最顶层的父窗口。
答案 1 :(得分:1)
<div onclick="parent.document.location.href='/cybersecurity/risk-management/index.cfm';" >
<b>Sample Text</b><br />
<span>
Sample Text
</span>
</div>
这会触发您对更改网址的父视图。
https://developer.mozilla.org/en-US/docs/Web/API/window.parent