javascript 获取 iframe 内的所有链接并添加空白目标属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8890699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all links inside iframe and add blank target attribute
提问by obautista
Is it possible to retrieve all href elements on a page that is an iframe?
是否可以检索 iframe 页面上的所有 href 元素?
Meaning, I have a page that is iframing a page from different site. What I am showing in the iframe contains a number of links (href tags).
意思是,我有一个页面正在对来自不同站点的页面进行 iframe。我在 iframe 中显示的内容包含许多链接(href 标签)。
Is it possible to add a TARGET attribute of BLANK to all links inside the iframe? The links are buried inside a number of divs and tables, so I need to be able to recursively add the attribute inside many nested tables/divs.
是否可以将 BLANK 的 TARGET 属性添加到 iframe 内的所有链接?链接隐藏在许多 div 和表中,因此我需要能够在许多嵌套表/div 中递归添加属性。
EDIT: Added screenshot to show extra characters needed to be removed:
编辑:添加屏幕截图以显示需要删除的额外字符:
回答by f2lollpll
If the iframe src it's on the same domain you can easily access it by using document.getElementById("frameID").document and manipulate that.
如果 iframe src 位于同一个域中,您可以使用 document.getElementById("frameID").document 轻松访问它并对其进行操作。
If it's a different domain you may consider link the iframe to a script in your own domain, and make that script download the data from the remote domain and print out:)
如果是不同的域,您可以考虑将 iframe 链接到您自己域中的脚本,并使该脚本从远程域下载数据并打印出来:)
myHTMLpage.html
我的HTML页面.html
<html>
<head>
<title></title>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#ifr").load(function () {
var ifr = document.getElementById("ifr")
var anchors = ifr.contentDocument.getElementsByTagName("a");
for (var i in anchors) {
anchors[i].setAttribute("target", "_blank");
}
});
});
</script>
</head>
<body>
<iframe src="myOwnSite.aspx" width="900px" height="600px" id="ifr" ></iframe>
</body>
</html>
myOwnSite.aspx
我的网站.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyOwnSite.aspx.cs" Inherits="MyOwnSite" %>
myOwnSite.aspx.cs
myOwnSite.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
public partial class MyOwnSite : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Write(new WebClient().DownloadString("http://theExternalSiteHere.com"));
}
}
}
回答by Darin Dimitrov
No, this is not possible if the site inside the iframe is not on your domain which according to your description seems to be the case.
不,如果 iframe 内的站点不在您的域中,根据您的描述似乎是这种情况,则这是不可能的。
回答by Andy
Try adding <base target="_blank">
inside the head tag of your iframe.
尝试<base target="_blank">
在 iframe 的 head 标签内添加。
回答by Nidhi
This is what worked for me:
这对我有用:
var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); for (var i in anchors) { anchors[i].target = "_blank"; //.setAttribute("target", "_blank"); }
var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); for (var i in anchors) { anchors[i].target = "_blank"; //.setAttribute("target", "_blank"); }
I have IE8. The script for IE 6 and IE 7 can be different.
我有IE8。IE 6 和 IE 7 的脚本可以不同。