我需要在相同的父页面中打开链接,而不是在一个新页面中打开它。

注意:iframe和父页面是同一个域。


当前回答

如前所述,您可以使用目标属性,但从技术上讲,它在XHTML中已被弃用。这就需要使用javascript,通常是parent.window.location。

其他回答

试目标= " _top "

<a href="http://example.com" target="_top">
   This link will open in same but parent window of iframe.
</a>

使用JavaScript:

window.parent.location.href= "http://www.google.com";

我找到了一个简单的解决办法

<iframe class="embedded-content" sandbox="allow-top-navigation"></iframe>

allow-top-navigation 允许iframe更改parent.location。 更多信息https://javascript.info/cross-window-communication

使用目标属性:

<a target="_parent" href="http://url.org">link</a>

<a target="parent">将在新选项卡/窗口中打开链接…<a target="_parent">将在父/当前窗口中打开链接,而不会打开新的选项卡/窗口。唐't_forget_that_underscore !