window.onload = function() {
            var frames = document.getElementById("frame-main");            var doc = frames.contentWindow.document;
            if (!doc.getElementsByTagName) return;
            var anchors = doc.getElementsByTagName("a");
            for (var i = 0; i < anchors.length; i++) {
                var anchor = anchors[i];
                if (anchor.getAttribute("href"))
                    anchor.target = "_self";
               
            }        };页面中有个iframe:<div id="bg-wrapper">
<iframe src="../../2010/Default.aspx" name="frame-main" id="frame-main" frameborder="0" height="100%" scrolling="auto" width="100%"></iframe>
</div>我的问题是我通过window.onload控制了这个iframe的所有超链接,但是当iframe这个页面操作的时候,也就是iframe可能变成了其他页面,这个时候前面设置的超链接为自身就不好使了这个应该怎么解决???当iframe刷新的时候还能控制它的所有超链接求大神帮忙

解决方案 »

  1.   

    iframe有一个onload事件,你可以将对超链接的操作写到iframe的onload事件中,这样应该就可以了。
      

  2.   

    这个我试了,但是onload的方法要怎么写????<iframe src="../../2010/Default.aspx" name="frame-main" id="frame-main" frameborder="0" height="100%" scrolling="auto" width="100%" onload="frameload()"></iframe>
    function frameload() {
                var frames = document.getElementById("frame-main");            var doc = frames.contentWindow.document;
                if (!doc.getElementsByTagName) return;
                var anchors = doc.getElementsByTagName("a");
                for (var i = 0; i < anchors.length; i++) {
                    var anchor = anchors[i];
                    if (anchor.getAttribute("href"))
                        anchor.target = "_self";
                   
                }        };这样会报错说var doc = frames.contentWindow.document;拒绝访问。。应该怎么修改
      

  3.   

    1.html<!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="gb2312" />
    <title></title>
    <style>
    </style>
    </head>
    <body>
    <iframe src="2.html" frameborder="1" onload="fn(this)"></iframe>
    <script type="text/javascript">
    var fn = function(t){
    var doc = t.contentWindow.document;
    var anchors = doc.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
    var anchor = anchors[i];
    console.log(1)
    if (anchor.getAttribute("href"))
    anchor.target = "_self";
    }
    }
    </script>
    </body>
    </html>
    2.html<!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="gb2312" />
    <title></title>
    <style>

    </style>
    </head>
    <body>
    <a href="http://baidu.com/" target="_blank">baidu</a>
    </body>
    </html>
    这样试试
      

  4.   

    js在获取页面的iframe的时候为什么老是报错说为空。。比如我var frames = document.getElementById("frame-main");这个写在onload外面就会报错