I have a question: How to get span content and store it in an array_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:04:23
Original
1534 people have browsed it

                        
                             
           
                2  
                United States
            
            
                3  
                Britain
            
            
                4  
                Germany
            
        
    

 
请教个问题,获取ul下li的值问题,我使用下面的代码获取到li值了,但包含着span呢,调试输出li内容时输出结果为:
4   Germany
其实我想获取到span里的内容,再去掉 后存到数组,也就是取出4、Germany存到数组。目前只会这些了,后面的不会了,请教下,这该怎么实现啊?

 var ulNode = document.getElementById("rightList");
  var childrens = ulNode.childNodes;
  var len = childrens.length;
  for(var i=0;i   {
      //alert(childrens[i].nodeName);
      //alert(childrens[i].innerHTML); //这两条语句是调试用的,想把span内容存到数组里,随表单提交,该怎么实现啊  
  }


回复讨论(解决方案)

<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>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template