使用 javascript 编辑所有外部链接

发布于 2024-07-08 05:37:54 字数 218 浏览 7 评论 0原文

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

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

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

How can I go through all external links in a div with javascript, adding (or appending) a class and alt-text?

I guess I need to fetch all objects inside the div element, then check if each object is a , and check if the href attributen starts with http(s):// (should then be an external link), then add content to the alt and class attribute (if they don't exist create them, if they do exists; append the wanted values).

But, how do I do this in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狠疯拽 2024-07-15 05:37:54

这个已经过测试:

<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.com,您可能需要更改 RE http://bar.my-server.com 标记为本地。

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

This one is tested:

<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>

If you are on an account on a shared server, like http://big-server.com/~UserName/, you might want to hard-code the URL to go beyond the top level. On the other hand, you might want to alter the RE if you want http://foo.my-server.com and http://bar.my-server.com marked as local.

[UPDATE] Improved robustness after good remarks...
I don't highlight FTP or other protocols, they probably deserve a distinct routine.

‘画卷フ 2024-07-15 05:37:54

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

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 是否是您要查找的元素

I think something like this could be a starting point:

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');
   }
}

you could also loop through all the A elements in the document, and check the parent to see if the div is the one you are looking for

绝情姑娘 2024-07-15 05:37:54

使用 Jquery 可以轻松完成此操作。 您可以将其添加到 onload:

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

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

This can be accomplished pretty easily with Jquery. You would add this to the onload:

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

You could modify this to add text. Class can also be modified using the css function.

为人所爱 2024-07-15 05:37:54

我的(非框架)方法是这样的:

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";
             } 
       }
   }

这没有经过测试,但我会像这样开始并从这里调试它。

My (non-framework) approach would be something along the lines of:

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";
             } 
       }
   }

This is not tested but I would start like this and debug it from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文