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)
所有浏览器都完全支持此标记.
nne*_*ala 138
使用target-attribute:
<a target="_parent" href="http://url.org">link</a>
Run Code Online (Sandbox Code Playgroud)
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中的所有链接都将在外部打开.
尝试里面的target="_parent" 属性anchor tag.
如上所述,您可以使用目标属性,但在技术上已弃用XHTML.这让你使用javascript,通常是类似的东西parent.window.location.
最通用和最跨浏览器的解决方案是避免使用“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