JS iframe 超链接
场景:js控制iframe的超链接都不弹出新窗口
【求助】js控制iframe的超链接都不弹出新窗口
页面中有个iframe:
我的问题是我通过window.onload控制了这个iframe的所有超链接,但是当iframe这个页面操作的时候,也就是iframe可能变成了其他页面,这个时候前面设置的超链接为自身就不好使了。。。
这个应该怎么解决???当iframe刷新的时候还能控制它的所有超链接
求大神帮忙。。。
------解决方案--------------------
iframe有一个onload事件,你可以将对超链接的操作写到iframe的onload事件中,这样应该就可以了。
------解决方案--------------------
1.html
【求助】js控制iframe的超链接都不弹出新窗口
- JScript code
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:
- HTML code
<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刷新的时候还能控制它的所有超链接
求大神帮忙。。。
------解决方案--------------------
iframe有一个onload事件,你可以将对超链接的操作写到iframe的onload事件中,这样应该就可以了。
------解决方案--------------------
1.html
- HTML code
<!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>