【问题标题】:Get all links inside iframe and add blank target attribute获取 iframe 内的所有链接并添加空白目标属性
【发布时间】:2012-02-12 00:36:13
【问题描述】:

是否可以检索 iframe 页面上的所有 href 元素?

意思是,我有一个页面是 iframe 来自不同站点的页面。我在 iframe 中显示的内容包含许多链接(href 标签)。

是否可以将 BLANK 的 TARGET 属性添加到 iframe 内的所有链接?链接隐藏在许多 div 和表中,因此我需要能够在许多嵌套表/div 中递归地添加属性。

编辑: 添加屏幕截图以显示需要删除的额外字符:

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    不,如果 iframe 内的网站不在您的域中,根据您的描述,这是不可能的。

    【讨论】:

      【解决方案2】:

      如果 iframe src 在同一个域上,您可以使用 document.getElementById("frameID").document 轻松访问它并对其进行操作。

      如果是不同的域,您可以考虑将 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 的页面。 iframes src 属性指向您自己站点上的 php 或 asp 脚本。该脚本将下载应该在 iframe 中的页面并 echo/response.write 出来。现在您在自己的域中拥有了正确的 html。我在我的手机上,所以我不能提供代码示例。但如果仍然没有意义,我可以在半小时内为您提供样品:)
      • 几乎......它只是没有将目标属性添加到href标签。我已经尝试过您的代码并使用 jquery。
      • 现在试试更新的 html 页面。有一些错误。我已经在 Chrome 16.0.9 中尝试过,它可以工作!此页面上的测试; msdn.microsoft.com/en-us/library/ff818516(v=vs.85).aspx 好奇
      • 看起来像编码问题。尝试阅读 html 中的字符编码 :)
      【解决方案3】:

      尝试在 iframe 的 head 标签内添加 &lt;base target="_blank"&gt;

      【讨论】:

        【解决方案4】:

        这对我有用:

        var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); for (var i in anchors) { 锚点[i].target = "_blank"; //.setAttribute("target", "_blank"); }

        我有 IE8。 IE 6 和 IE 7 的脚本可以不同。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-02
          • 1970-01-01
          • 1970-01-01
          • 2016-11-21
          • 2015-06-22
          相关资源
          最近更新 更多