根据标签名获取网页中的多个元素

时间:2020-12-22 05:07:07
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>水果</title>
<script>
function getDomsByTag(){
var doms=document.getElementsByTagName("span");
var str='';
for(i=0;i<doms.length;i++){
str+=doms[i].innerHTML;
str+=",";
}
alert("两个DOM的文本是:"+str);
}
</script>
</head>
<body>
<p>
<span>香蕉</span>
<span>苹果</span>
<input type="button" value="获取" onclick="getDomsByTag()">
</p>
</body>

</html>

结果截图:

根据标签名获取网页中的多个元素