在iframe页面中,是否可以检索到所有href元素?
也就是说,我有一个页面,它嵌入了来自不同站点的页面。在iframe中显示的内容包含许多链接(href标签)。
是否可以将所有链接内的TARGET属性添加为BLANK?由于链接深埋在许多嵌套的表格和div中,因此我需要能够递归地在其中添加属性。
编辑:添加截图以显示需要删除的额外字符:
在iframe页面中,是否可以检索到所有href元素?
也就是说,我有一个页面,它嵌入了来自不同站点的页面。在iframe中显示的内容包含许多链接(href标签)。
是否可以将所有链接内的TARGET属性添加为BLANK?由于链接深埋在许多嵌套的表格和div中,因此我需要能够递归地在其中添加属性。
编辑:添加截图以显示需要删除的额外字符:
<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
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyOwnSite.aspx.cs" Inherits="MyOwnSite" %>
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"));
}
}
}
<base target="_blank">
。以下是对我有效的方法:
var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); for (var i in anchors) { anchors[i].target = "_blank"; //.setAttribute("target", "_blank"); }
我使用的是IE8。IE6和IE7的脚本可能不同。