我创建了一个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" />,但它不起作用。
发布于 2013-06-07 15:13:28
您希望设置最顶部窗口(父窗口)的位置,而不是设置window.location.href,它是当前窗口( iframe)。
window.top.location.href = 'foobar'你也可以使用window.parent,它将使用当前窗口的直接父窗口,但在大多数情况下,使用window.top更安全,因为你实际上想要在大多数父窗口之上。
发布于 2013-06-07 15:13:38
<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
https://stackoverflow.com/questions/16978213
复制相似问题