Js通过class取标签简单实例
|
阅读数:--次|
作者:小猪仔
摘要:Js通过class取标签简单实例,原生实现没有用jquery
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Js通过class取标签简单实例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="div1">测试</div>
<div class="div1">测试</div>
<div class="div1">测试</div>
<div class="div1">测试</div>
<p>结果:<p/>
<div id="jgtextid">
</div>
<script type="text/javascript">
//取页面所有class为div1的标签反回数组
var arr=document.querySelectorAll(".div1");
var html=[];
for(var i=0;i<arr.length;i++){
var obj=arr[i];
html[html.length]="div"+(i+1)+":"+obj.innerHTML+"<br/>";
}
document.getElementById("jgtextid").innerHTML=html.join('');
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Js通过class取标签简单实例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="div1">测试</div>
<div class="div1">测试</div>
<div class="div1">测试</div>
<div class="div1">测试</div>
<p>结果:<p/>
<div id="jgtextid">
</div>
<script type="text/javascript">
//取页面所有class为div1的标签反回数组
var arr=document.querySelectorAll(".div1");
var html=[];
for(var i=0;i<arr.length;i++){
var obj=arr[i];
html[html.length]="div"+(i+1)+":"+obj.innerHTML+"<br/>";
}
document.getElementById("jgtextid").innerHTML=html.join('');
</script>
</body>
</html>