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

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


当前回答

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

<base target="_parent">

这将在父窗口中加载页面上的所有链接。如果你想要你的链接加载在一个新的窗口,使用:

<base target="_blank">

浏览器支持

其他回答

使用JavaScript:

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

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

是的,我找到了

<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>
   <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>