我需要在相同的父页面中打开链接,而不是在一个新页面中打开它。
注意:iframe和父页面是同一个域。
我需要在相同的父页面中打开链接,而不是在一个新页面中打开它。
注意:iframe和父页面是同一个域。
当前回答
你可以使用任何选项
如果只有父页面:
如果你想打开父页面或父iframe的所有链接,那么你可以在iframe的头部部分使用以下代码:
<base target="_parent" />
OR
如果你想打开一个特定的链接到父页面或父iframe,那么你可以使用以下方法:
<a target="_parent" href="http://specific.org">specific Link</a>
<a href="http://normal.org">Normal Link</a>
OR
在嵌套iframe的情况下:
如果想打开所有链接到浏览器窗口(重定向在浏览器url),那么你在iframe头部部分使用以下代码:
<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>
OR
如果你想打开一个特定的链接到浏览器窗口(重定向在浏览器url),那么你可以使用以下方式:
<a href="http://specific.org" target="_top" >specific Link</a>
or
<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>
其他回答
有一个HTML元素叫做base,它允许你:
为页面上的所有链接指定默认URL和默认目标:
<base target="_blank" />
通过指定_blank,可以确保iframe内的所有链接都将在外部打开。
<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>
是的,我找到了
<base target="_parent" />
这对于打开在iframe中打开的所有iframe链接很有用。
And
$(window).load(function(){
$("a").click(function(){
top.window.location.href=$(this).attr("href");
return true;
})
})
这可以用于整个页面,也可以用于页面的特定部分。
谢谢你们的帮助。
使用目标属性:
<a target="_parent" href="http://url.org">link</a>
<a target="parent">将在新选项卡/窗口中打开链接…<a target="_parent">将在父/当前窗口中打开链接,而不会打开新的选项卡/窗口。唐't_forget_that_underscore !