简体   繁体   English

选择ul li层次结构中td的所有innerHTML

[英]Select all innerHTML of td in ul li hierarchy

I have an html structure as follows- 我有一个html结构,如下所示:

<ul id='abc'>
    <li>
        <table>
            <tbody>
                <tr>
                    <td>
                        A
                    </td>
                </tr>
            </tbody>
        </table>
    </li>
    <li>
        <table>
            <tbody>
                <tr>
                    <td>
                        B
                    </td>
                </tr>
            </tbody>
        </table>
    </li>       
    ...
    ...
</ul>

I want to store A,B,C... values in an array using jquery.Please help... 我想使用jquery将A,B,C ...值存储在数组中。请帮助...

You can use $.fn.map() 您可以使用$.fn.map()

Translate all items in an array or object to new array of items. 将数组或对象中的所有项目转换为新的项目数组。

var arr = $('#abc td').map(function(){
    return $(this).text();
}).get();

you can do this by javascript by The 您可以通过The Javascript执行此操作

Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name Element.getElementsByTagName()方法返回具有给定标记名称的实时HTMLCollection元素

var values = []
var tableUl = document.getElementById("abc"); 
var cells = tableUl.getElementsByTagName("td"); 
for (var i = 0; i < cells.length; i++) { 
    values.push( cells[i].textContent.trim()); 

}

fiddle 小提琴

Here is what you want: 这是您想要的:

var values = [];
$('ul li').find('td').each(function() {
  values.push($(this).html());
});

这应该工作:

var values = $('ul#abc td').map(function() { return $(this).text(); });

Try utilizing .text() , String.prototype.match() 尝试利用.text()String.prototype.match()

 var res = $("#abc td").text().match(/\\w+/gi) console.log(res) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <ul id='abc'> <li> <table> <tbody> <tr> <td> A </td> </tr> </tbody> </table> </li> <li> <table> <tbody> <tr> <td> B </td> </tr> </tbody> </table> </li> </ul> 

Another way would be: 另一种方法是:

$("ul#abc li td:first-child").each(function () {
   //alert($(this).text());
   console.log($(this).text());
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM