统计所有< a>在html页面上 [英] Count all <a> on html page
本文介绍了统计所有< a>在html页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 Html
页面中获取所有< a>
标签,并使用 JavaScript
方法:
function(){
var links = document.getElementsByTagName ('一个');
var i = 0;
for(var link in links){i ++;}
return i;
}
我注意到它不会返回正确数量的标签。 / p>
有什么想法可以解决问题吗?
如果有任何其他方法可以获得所有的href在 Html
?
编辑
这个方法在这个html: http://mp3skull.com/mp3/nirvana.html 上。
我得到这个结果:1。但在页面中有更多结果。
解决方案
这里不需要循环。您可以阅读 length
属性。
function getACount(){
return document.getElementsByTagName('a')。length;
}
I want to get all the <a>
tags from an Html
page with this JavaScript
method:
function() {
var links = document.getElementsByTagName('a');
var i=0;
for (var link in links){i++;}
return i;
}
And i noticed it's won't return the correct number of a tags.
Any idea what can by the problem?
Any idea if there is any other way to get all the href in an Html
?
Edit
I tried this method on this html : http://mp3skull.com/mp3/nirvana.html . And i get this result:"1". but there are more results in the page.
解决方案
You don't need a loop here. You can read length
property.
function getACount() {
return document.getElementsByTagName('a').length;
}
这篇关于统计所有< a>在html页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文