[英]Get all links inside iframe and add blank target attribute
是否可以檢索作為iframe的頁面上的所有href元素?
意思是,我有一個頁面來自不同的網站iframing頁面。 我在iframe中顯示的內容包含許多鏈接(href標記)。
是否可以將BLANK的TARGET屬性添加到iframe中的所有鏈接? 鏈接隱藏在許多div和表中,因此我需要能夠在許多嵌套表/ div中遞歸地添加屬性。
編輯:添加了截圖以顯示需要刪除的額外字符:
如果iframe src位於同一個域中,您可以使用document.getElementById(“frameID”)輕松訪問它。記錄並操作它。
如果它是一個不同的域,您可以考慮將iframe鏈接到您自己域中的腳本,並使該腳本從遠程域下載數據並打印出來:)
myHTMLpage.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
<%@ 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"));
}
}
}
不,如果iframe中的網站不在您的域中,根據您的描述似乎是這種情況,這是不可能的。
嘗試在iframe的head標記內添加<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。 IE 6和IE 7的腳本可以不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.