javascript - 获取所有锚标记并将它们与数组进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3844739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:26:49  来源:igfitidea点击:

javascript - get all anchor tags and compare them to an array

javascript

提问by Dr Hydralisk

I have been trying forever but it is just not working, how can I check the array of urls I got (document.getElementsByTagName('a').href;) to see if any of the websites are in another array?

我一直在尝试,但它只是不起作用,我如何检查我得到的 url 数组 ( document.getElementsByTagName('a').href;) 以查看是否有任何网站在另一个数组中?

回答by some

getElementByTagNamegives you a nodelist (an array of nodes).

getElementByTagName为您提供一个节点列表(一个节点数组)。

var a = document.getElementsByTagName('a');

for (var idx= 0; idx < a.length; ++idx){
    console.log(a[idx].href);
}

I really suggest that you use a frame work for this, like jquery. It makes your life so much easier.

我真的建议您为此使用框架工作,例如 jquery。它让你的生活变得更轻松。

Example with jquery:

jQuery 示例:

$("a").each(function(){
  console.log(this.href);
});

回答by Gabriel

var linkcheck = (function(){
  if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
  for(var i=0; i<this.length; i++){
    if(this[i]===obj){
      return i;
    }
  }
      return -1;
    }
  }
  var url_pages = [], anchor_nodes = []; // this is where you put the resulting urls
  var anchors = document.links; // your anchor collection
  var i = anchors.length;
  while (i--){
    var a = anchors[i];
    anchor_nodes.push(a); // push the node object in case that needs to change
    url_pages.push(a.href); // push the href attribute to the array of hrefs
  }
  return {
    urlsOnPage: url_pages, 
    anchorTags: anchor_nodes, 
    checkDuplicateUrls: function(url_list){
      var duplicates = []; // instantiate a blank array
      var j = url_list.length;
      while(j--){
        var x = url_list[j];
        if (url_pages.indexOf(x) > -1){ // check the index of each item in the array.
          duplicates.push(x); // add it to the list of duplicate urls
        }
      }
      return duplicates; // return the list of duplicates.
    },
    getAnchorsForUrl: function(url){
      return anchor_nodes[url_pages.indexOf(url)];
    }
  }
})()
// to use it:
var result = linkcheck.checkDuplicateUrls(your_array_of_urls);

This is a fairly straight forward implementation of a pure JavaScript method for achieving what I believe the spec calls for. This also uses closures to give you access to the result set at any time, in case your list of urls changes over time and the new list needs to be checked. I also added the resulting anchor tags as an array, since we are iterating them anyway, so you can change their properties on the fly. And since it might be useful to have there is a convenience method for getting the anchor tag by passing the url (first one in the result set). Per the comments below, included snippet to create indexOf for IE8 and switched document.getElementsByTagName to document.links to get dynamic list of objects.

这是一个相当直接的纯 JavaScript 方法实现,用于实现我认为规范所要求的内容。这也使用闭包让您可以随时访问结果集,以防您的 url 列表随时间发生变化并且需要检查新列表。我还将生成的锚标记添加为数组,因为无论如何我们都在迭代它们,因此您可以即时更改它们的属性。并且因为有一种通过传递 url(结果集中的第一个)来获取锚标记的便捷方法可能很有用。根据下面的评论,包括为 IE8 创建 indexOf 的片段,并将 document.getElementsByTagName 切换为 document.links 以获取对象的动态列表。

回答by Vinay B R

Using Jquery u can do some thing like this-

使用 Jquery 你可以做这样的事情 -

$('a').each(function(){
if( urls.indexOf(this.href) !- -1 )
    alert('match found - ' + this.href );
})

urls is the your existing array you need to compare with.

urls 是您需要比较的现有数组。