如何强制在父窗口中打开iframe的链接

Hai*_*vgi 340 html iframe hyperlink html-target

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

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

Chr*_*lli 594

我发现最好的解决方案是使用基本标签.将以下内容添加到iframe页面的头部:

<base target="_parent">
Run Code Online (Sandbox Code Playgroud)

这将加载父窗口中页面上的所有链接.如果您希望在新窗口中加载链接,请使用:

<base target="_blank">
Run Code Online (Sandbox Code Playgroud)

所有浏览器都完全支持此标记.

  • 为什么你在**回答之后一年回答这个问题,答案完全相同?但你被选中了.这太奇怪了. (5认同)
  • 这种票价如何跨浏览器? (4认同)
  • @Wilf我不太正确:;)`<base target>`应该在任何`<a href>`之前,因为它设置了以下链接的默认浏览上下文; `<base href>`应该在任何URL引用之前(`<*href>``<*src>``<form action>``<object data>`...),因为它设置了相对URL所针对的基本URL解决. (3认同)
  • 我认为这是因为你的答案会在新窗口中打开链接,而我会在iframe的父窗口中打开它,这是原始问题.抱歉偷你的雷声! (2认同)

nne*_*ala 138

使用target-attribute:

<a target="_parent" href="http://url.org">link</a>
Run Code Online (Sandbox Code Playgroud)

  • `<base target ="_ blank">`是好的,但这里的问题是关于一个链接,而不是关于改变iframe上所有链接的行为.对我来说,这是正确的答案. (5认同)
  • 来自我的+1,基础目标="_父母"有效,但目标="_父母"对我来说从未失败过! (2认同)

yen*_*sen 32

使用JavaScript:

window.parent.location.href= "http://www.google.com";
Run Code Online (Sandbox Code Playgroud)


Aho*_*sik 25

您可以使用任何选项

如果只有父页面:

如果你想打开所有链接到父页面或父iframe,那么你在iframe的head部分使用以下代码:

<base target="_parent" />

要么

如果要打开父页面或父iframe 的特定链接,则使用以下方式:

<a target="_parent" href="http://specific.org">specific Link</a>
<a  href="http://normal.org">Normal Link</a>
Run Code Online (Sandbox Code Playgroud)

要么

在嵌套iframe的情况下:

如果要打开所有链接到浏览器窗口(在浏览器URL中重定向),那么您在iframe的head部分中使用以下代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
    $(window).load(function(){
        $("a").click(function(){
            top.window.location.href=$(this).attr("href");
            return true;
        })
    })
</script>
Run Code Online (Sandbox Code Playgroud)

要么

如果要打开浏览器窗口中的特定链接(在浏览器URL中重定向),则使用以下方式:

<a  href="http://specific.org"   target="_top" >specific Link</a>
Run Code Online (Sandbox Code Playgroud)

要么

<a  href="javascript:top.window.location.href='your_link'">specific Link</a>
<a  href="javascript:top.window.location.href='http://specific.org'">specific Link</a>
<a  href="http://normal.org">Normal Link</a>
Run Code Online (Sandbox Code Playgroud)


vsy*_*ync 15

有一个HTML元素base,它允许您:

为页面上的所有链接指定默认URL和默认目标:

<base target="_blank" />
Run Code Online (Sandbox Code Playgroud)

通过指定_blank您确保iframe中的所有链接都将在外部打开.


Gee*_*Gee 7

尝试里面的target="_parent" 属性anchor tag.

  • 尝试target ="_ top".iframe可能包含嵌套的iframe?或者在onclick事件上通过javascript打开实际链接? (4认同)

Rya*_*ath 7

如上所述,您可以使用目标属性,但在技术上已弃用XHTML.这让你使用javascript,通常是类似的东西parent.window.location.

  • [不再推荐使用`target`属性](http://dev.w3.org/html5/markup/a.html#a.attrs.target). (10认同)

Ade*_*mar 6

最通用和最跨浏览器的解决方案是避免使用“base”标签,而是使用“a”标签的目标属性:

<a target="_parent" href="http://www.stackoverflow.com">Stack Overflow</a>
Run Code Online (Sandbox Code Playgroud)

<base>标签是通用性较差,而浏览器是在他们对文档中的位置要求不符,需要更多的跨浏览器测试。根据您的项目和情况,实现理想的<base>标签跨浏览器放置可能很困难,甚至完全不可行。

用标签的target="_parent"属性这样做<a>不仅对浏览器更友好,而且可以让你区分那些你想在 iframe 中打开的链接,和那些你想在父级中打开的链接。


小智 6

如果您在网页中使用 iframe,则在通过 iframe 中的 HTML 超链接(锚标记)更改整个页面时可能会遇到问题。有两种解决方案可以缓解这个问题。

解决方案 1. 您可以使用以下示例中给出的锚标记的目标属性。

<a target="_parent" href="http://www.kriblog.com">link</a>
Run Code Online (Sandbox Code Playgroud)

解决方案 2. 您也可以使用 JavaScript 从 iframe 在父窗口中打开一个新页面。

<a href="#" onclick="window.parent.location.href='http://www.kriblog.com';">
Run Code Online (Sandbox Code Playgroud)

记住 ?target="_parent"在 XHTML 中已被弃用,但在 HTML 5.x 中仍受支持。

可以从以下链接阅读更多内容 http://www.kriblog.com/html/link-of-iframe-open-in-the-parent-window.html