【发布时间】:2010-11-05 11:43:07
【问题描述】:
我需要在同一个父页面中打开链接,而不是在新页面中打开它。
注意:iframe 和父页面是同一个域。
【问题讨论】:
标签: html iframe hyperlink html-target
我需要在同一个父页面中打开链接,而不是在新页面中打开它。
注意:iframe 和父页面是同一个域。
【问题讨论】:
标签: html iframe hyperlink html-target
使用目标属性:
<a target="_parent" href="http://url.org">link</a>
【讨论】:
<base target="_blank"> 可以,但这里的问题是关于一个链接,而不是关于改变 iframe 上所有链接的行为。对我来说,这是正确的答案。
在anchor tag 中尝试target="_parent" 属性。
【讨论】:
如前所述,您可以使用目标属性,但从技术上讲,它在 XHTML 中已被弃用。这样你就可以使用 javascript,通常是 parent.window.location。
【讨论】:
有一个名为 base 的 HTML 元素允许您:
为页面上的所有链接指定默认 URL 和默认目标:
<base target="_blank" />
通过指定_blank,您可以确保iframe 内的所有链接都将在外部打开。
【讨论】:
我发现最好的解决方案是使用 base 标签。将以下内容添加到 iframe 中的页面头部:
<base target="_parent">
这将加载父窗口中页面上的所有链接。如果您希望链接在新窗口中加载,请使用:
<base target="_blank">
【讨论】:
<base target> 应该出现在任何 <a href> 之前,因为它为以下链接设置了默认浏览上下文; <base href> 应该出现在任何 URL 引用之前(<* href> <* src> <form action> <object data>...),因为它设置了解析相对 URL 所依据的基本 URL。
<base 标签根据规范 w3.org/TR/html5/document-metadata#the-base-element 没有关闭,因此它应该是 <base target="_parent" >
使用 JavaScript:
window.parent.location.href= "http://www.google.com";
【讨论】:
<a target="parent"> 将在新选项卡/窗口中打开链接...<a target="_parent"> 将在父/当前窗口中打开链接,而不打开新选项卡/窗口。不要忘记那个下划线!
【讨论】:
最通用和最跨浏览器的解决方案是避免使用“base”标签,而是使用“a”标签的target属性:
<a target="_parent" href="http://www.stackoverflow.com">Stack Overflow</a>
<base> 标签的通用性较差,浏览器对其在文档中的放置要求不一致,需要更多的跨浏览器测试。根据您的项目和情况,实现<base> 标记的理想跨浏览器放置可能很困难,甚至完全不可行。
使用<a> 标记的target="_parent" 属性执行此操作不仅对浏览器更加友好,而且还允许您区分要在 iframe 中打开的链接和要在 iframe 中打开的链接父母。
【讨论】:
如果您在网页中使用 iframe,则在通过 iframe 中的 HTML 超链接(锚标记)更改整个页面时可能会遇到问题。有两种解决方案可以缓解这个问题。
解决方案1.可以使用如下示例中给出的锚标记的目标属性。
<a target="_parent" href="http://www.kriblog.com">link</a>
解决方案 2. 您也可以使用 JavaScript 从 iframe 在父窗口中打开新页面。
<a href="#" onclick="window.parent.location.href='http://www.kriblog.com';">
请记住 ⇒ target="_parent" 在 XHTML 中已被弃用,但在 HTML 5.x 中仍受支持。
更多内容可以从以下链接阅读 http://www.kriblog.com/html/link-of-iframe-open-in-the-parent-window.html
【讨论】:
您可以使用任何选项
如果只有父页面:
如果你想在父页面或父 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>
或
在嵌套 iframe 的情况下:
如果想在浏览器窗口中打开所有链接(在浏览器网址中重定向),那么您可以在 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>
或
如果您想在浏览器窗口中打开一个特定链接(在浏览器网址中重定向),那么您可以使用以下方式:
<a href="http://specific.org" target="_top" >specific Link</a>
或
<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>
【讨论】:
是的,我找到了
<base target="_parent" />
这对于打开在 iframe 中打开的所有 iframe 链接很有用。
还有
$(window).load(function(){
$("a").click(function(){
top.window.location.href=$(this).attr("href");
return true;
})
})
我们可以将其用于整个页面或页面的特定部分。
感谢大家的帮助。
【讨论】:
<script type="text/javascript"> // if site open in iframe then redirect to main site
$(function(){
if(window.top.location != window.self.location)
{
top.window.location.href = window.self.location;
}
});
</script>
【讨论】:
试试target="_top"
<a href="http://example.com" target="_top">
This link will open in same but parent window of iframe.
</a>
【讨论】:
"_top" 和"_parent" 一样吗?