1

如何使用 javascript 浏览 div 中的所有外部链接,添加(或附加)一个类和替代文本?

我想我需要获取 div 元素内的所有对象,然后检查每个对象是否为 a ,并检查 href 属性是否以 http(s):// 开头(应该是外部链接),然后将内容添加到alt 和 class 属性(如果它们不存在,则创建它们,如果它们存在;附加想要的值)。

但是,我如何在代码中做到这一点?

4

4 回答 4

2

这个经过测试:

<style type="text/css">
.AddedClass
{
  background-color: #88FF99;
}
</style>
<script type="text/javascript">
window.onload = function ()
{
  var re = /^(https?:\/\/[^\/]+).*$/;
  var currentHref = window.location.href.replace(re, '$1');
  var reLocal = new RegExp('^' + currentHref.replace(/\./, '\\.'));

  var linksDiv = document.getElementById("Links");
  if (linksDiv == null) return;
  var links = linksDiv.getElementsByTagName("a");
  for (var i = 0; i < links.length; i++)
  {
    var href = links[i].href;
    if (href == '' || reLocal.test(href) || !/^http/.test(href))
      continue;
    if (links[i].className != undefined)
    {
      links[i].className += ' AddedClass';
    }
    else
    {
      links[i].className = 'AddedClass';
    }
    if (links[i].title != undefined && links[i].title != '')
    {
      links[i].title += ' (outside link)';
    }
    else
    {
      links[i].title = 'Outside link';
    }
  }
}
</script>

<div id="Links">
<a name="_Links"></a>
<a href="/foo.asp">FOO</a>
<a href="ftp://FTP.org/FILE.zip">FILE</a>
<a href="http://example.com/somewhere.html">SomeWhere</a>
<a href="http://example.com/somewhere2.html" class="Gah">SomeWhere 2</a>
<a href="http://example.com/somewhere3.html" title="It goes somewhere">SomeWhere 3</a>
<a href="https://another-example.com/elsewhere.php?foo=bar">ElseWhere 1</a>
<a href="https://another-example.com/elsewhere.php?foo=boz" class="Doh">ElseWhere 2</a>
<a href="https://another-example.com/elsewhere.php?foo=rad" title="It goes elsewhere">ElseWhere 3</a>
<a href="deep/below/bar.asp">BAR</a>
<a href="javascript:ShowHideElement('me');">Show/Hide</a>
</div>

如果您使用的是共享服务器上的帐户,例如http://big-server.com/~UserName/,您可能希望对 URL 进行硬编码以超出顶层。另一方面,如果要将http://foo.my-server.comhttp://bar.my-server.com标记为本地,则可能需要更改 RE。

[更新] 好话之后提高了健壮性...
我不强调 FTP 或其他协议,它们可能应该有一个独特的例程。

于 2008-11-14T17:45:59.283 回答
1

我认为这样的事情可能是一个起点:

var links = document.getElementsByTagName("a"); //use div object here instead of document
for (var i=0; i<links.length; i++)
{
   if (links[i].href.substring(0, 5) == 'https')
   {
      links[i].setAttribute('title', 'abc');
      links[i].setAttribute('class', 'abc');
      links[i].setAttribute('className', 'abc');
   }
}

您还可以遍历文档中的所有 A 元素,并检查父元素以查看 div 是否是您要查找的元素

于 2008-11-14T15:48:14.853 回答
0

这可以通过Jquery轻松完成。您可以将此添加到 onload:

$("div a[href^='http']").each(function() {
  $(this).attr("alt",altText);
  var oldClassAttributeValue = $(this).attr("class");
  if(!oldClassAttributeValue) {
   $(this).attr("class",newClassAttributeValue);
  }
});

您可以修改它以添加文本。也可以使用css函数修改类。

于 2008-11-14T15:36:41.757 回答
0

我的(非框架)方法将类似于:

window.onload = function(){
   targetDiv = document.getElementById("divName");
   linksArray = targetDiv.getElementsByTagName("a");
   for(i=0;i=linksArray.length;i++){
      thisLink = linksArray[i].href;
      if(thisLink.substring(4,0) = "http"){
         linksArray[i].className += "yourcontent";  //you said append so +=
         linksArray[i].alt += "yourcontent";
             } 
       }
   }

这未经测试,但我会这样开始并从这里调试它。

于 2008-11-14T15:57:46.520 回答