开发者

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

I have been trying forever but i开发者_如何学运维t 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?


getElementByTagName gives you a nodelist (an array of nodes).

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.

Example with jquery:

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


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.


Using Jquery u can do some thing like this-

$('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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜