Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

javascript - Javascript同时从DOM循环ElementIds直到第一个空元素(Javascript while loop over ElementIds from DOM until first empty Element)

Easy task i guess but i am new to this and am stuck.(我想这很容易,但是我对此并不陌生,被困住了。)

I want to iterate through some ElementIds of a webpage to extract the text within.(我想遍历网页的一些ElementId以提取其中的文本。) The ElementIds i am looking for are like this:(我正在寻找的ElementIds像这样:) "p000", "p001", "p002", etc. (I guess it would go until "p999")(“ p000”,“ p001”,“ p002”等(我想它会一直到“ p999”)) This is what i have so far.(这是我到目前为止所拥有的。) var totalText=[] while(text===""): var text=document.getElementById("p0003"); totalText.push(text); } Now i would like to iterate through the ElementIds as described and stop as soon as text is empty which means there is no Element "p091" anymore and the loop stops and does not continue until "p999".(现在,我想按上述说明遍历ElementIds,并在文本为空时立即停止,这意味着不再有元素“ p091”,并且循环停止并且直到“ p999”才继续。)   ask by Mauritius translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...