将iframe链接加载到父窗口?

时间:2013-03-29 23:12:43

标签: html iframe

我有这个iframe代码:

<iframe src="http://www.example.com" border="0" framespacing="0" marginheight="0" marginwidth="0" vspace="0" hspace="0" scrolling="yes" width="100%" frameborder="0" height="100%"></iframe>

我想要做的是..当我点击iframe页面中的任何链接..然后我会转到它并且页面将加载,我将从当前页面到iframe链接作为新页面

target="_self"这样的东西 - 不像普通的iframe那样会在浏览器本身没有任何变化的情况下进入此链接。

示例: iframe src:http://www.w3schools.com

当我点击iframe页面内的“学习HTML”时...然后我会转到http://www.w3schools.com/html/default.asp 我的浏览器网址也会更改为。

所以我只是点击了iframe中的一个链接,然后我就去了。

有什么想法吗?

5 个答案:

答案 0 :(得分:38)

是的,您可以使用target="_parent"来实现这一目标。

  

“target =”_ parent“打开父框架中的链接文档。”

示例:

<a target="_parent" href="http://example.org">Click me!</a>

修改

如果这不起作用,您可以尝试_top

<a target="_top" href="http://example.org">Click me!</a>

base target

<base target="_parent" />

JavaScript中的window.top.location

window.top.location = "http://example.com";

答案 1 :(得分:14)

您正在寻找的是<a href="..." target="_parent">

_parent会使其转到父框架

_top会使其进入最高级别。

_parent_top适用于您的目标。

答案 2 :(得分:6)

您可以使用javascript动态地将目标添加到链接中,如下所示。

function onLoadIF(frame)
{
    var elements = frame.getElementsByTagName('a'),
        len = elements.length;

    while( len-- ) {
        elements[len].target = "_parent";
    }
}

然后将onload =“onLoadIF(this)”添加到iframe。

答案 3 :(得分:5)

是的,只需在链接上添加target="_parent"即可在主页上加载。

答案 4 :(得分:1)

如果网站在iframe中打开,则会重定向到主网站

<script>
    if (window.top.location != window.self.location) {
        top.window.location.href = window.self.location;
    }
</script>