Rumah >hujung hadapan web >html tutorial >请教个问题:如何获取span内容并存到数组里_html/css_WEB-ITnose
<div id="sample_2" class="sampleDiv"><ul id="leftList"> <li class="simpleItem"> <span class="idCol">2 </span> <span class="nameCol">United States</span> </li> <li class="simpleItem"> <span class="idCol">3 </span> <span class="nameCol">Britain</span> </li> <li class="simpleItem"> <span class="idCol">4 </span> <span class="nameCol">Germany</span> </li> </ul> </div> <script> var lis = document.getElementById('leftList').getElementsByTagName('li'); var rst = []; for (var i = 0; i < lis.length; i++) { rst.push(lis[i].innerHTML.replace(/<[^>]+>| |\s/gi, ''));//用正则去掉html标签和 及空白内容 } alert(rst.join('\n')) </script>