This will help just get all elements and loop through them!(这将有助于获取所有元素并遍历它们!)
var arrayofelements = []; function getElements(){ var lenght=document.getElementsByTagName("div").length; for(var a = 0;a <= lenght; a++){ if(a == 0){ a = "000"; } else if(a <= 9){ a = "00"+a; } else if(a => 10 && a < 99){ a = "0"+a; } var text = document.getElementById("p"+a); if(text == undefined || text == null){ return; } arrayofelements.push(text.innerHTML); } } getElements(); arrayofelements.forEach(foreachfunc); function foreachfunc(item,index){ document.write("Array Data: "+item); }
<div id="p000">0</div> <div id="p001">1</div> <div id="p002">2</div> <div id="p003">3</div> <div id="p004">4</div> <div id="p005">5</div> <div id="p006">6</div> <div id="p007">7</div> <div id="p008">8</div> <div id="p009">9</div> <div id="p010">10</div> <div id="p011">11</div> <div id="p012">12</div> <div id="p013">13</div> <div id="p014">14</div> <div id="p015">15</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…