Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-获取所有锚定标记并将它们与数组进行比较_Javascript - Fatal编程技术网

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

javascript-获取所有锚定标记并将它们与数组进行比较,javascript,Javascript,我一直在尝试,但它只是不起作用,我如何检查我得到的URL数组(document.getElementsByTagName('a').href;)以查看是否有任何网站在另一个数组中?getElementByTagName为您提供一个节点列表(一个节点数组) var linkcheck=(函数(){ if(!Array.indexOf){ Array.prototype.indexOf=函数(obj){ 对于(vari=0;i-1){//检查数组中每个项的索引。 duplicates.push(x)

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

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

var linkcheck=(函数(){
if(!Array.indexOf){
Array.prototype.indexOf=函数(obj){
对于(vari=0;i-1){//检查数组中每个项的索引。
duplicates.push(x);//将其添加到重复URL列表中
}
}
返回重复项;//返回重复项列表。
},
GetAnchorForURL:函数(url){
返回锚节点[url\u pages.indexOf(url)];
}
}
})()
//要使用它:
var result=linkcheck.checkDuplicateURL(您的URL数组);

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

使用Jquery u可以执行类似的操作-

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

URL是您需要与之进行比较的现有数组。

这非常简单——使用jQuery真的会更容易吗?我认为实际上可能需要更多的字符才能全部使用“$().attr('blahblah')等等@Mark:我添加了一个jquery示例。正如您所看到的,它的类型较少。请注意,Array.prototype.indexOf()未在IE 8及更早版本中实现,需要一个额外的代码段来定义该方法。还要注意,您的列表是静态的。您可以使用document.links在页面上获取a和AREA元素的动态集合。@Andy E感谢您提供的信息。我想当然地认为indexOf实现无处不在。最近我一直在使用jQuery,所以我把document.links完全隔开了。
$("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);
$('a').each(function(){
if( urls.indexOf(this.href) !- -1 )
    alert('match found - ' + this.href );
})