Javascript 使用JQuery获取单元格值

Javascript 使用JQuery获取单元格值,javascript,jquery,html,Javascript,Jquery,Html,使用JQuery获取单元格值 我已尝试使用以下代码: $("#table tr").each(function(){ var result = $(this).find("td:first").html(); alert(result); }); 但它返回所有第一行的字符串 <table class="table table-bordered"> <thead> <tr>

使用JQuery获取单元格值

我已尝试使用以下代码:

$("#table tr").each(function(){
    var result = $(this).find("td:first").html();
    alert(result);
});
但它返回所有第一行的字符串

<table class="table table-bordered">
        <thead>
            <tr>
                <td style="white-space: nowrap" class="form-label">
                    <span id="lblAppMonth1HeaderYr1" class="form-label-bold"></span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth2HeaderYr1" class="form-label-bold"></span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth3HeaderYr1" class="form-label-bold">Jun-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth4HeaderYr1" class="form-label-bold">Jul-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth5HeaderYr1" class="form-label-bold">Aug-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth6HeaderYr1" class="form-label-bold">Sep-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth7HeaderYr1" class="form-label-bold">Oct-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth8HeaderYr1" class="form-label-bold">Nov-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth9HeaderYr1" class="form-label-bold">Dec-17</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth10HeaderYr1" class="form-label-bold">Jan-18</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth11HeaderYr1" class="form-label-bold">Feb-18</span>
                </td>
                <td style="white-space: nowrap">
                    <span id="lblAppMonth12HeaderYr1" class="form-label-bold">Mar-18</span>
                </td>
            </tr>
        </thead>
        <tbody>

六月十七日
七月十七日
8月17日
九月十七日
10月17日
11月17日
十二月十七日
一月十八日
二月十八日
三月十八日
我期望值“6月17日”、“7月17日”。。。。按那个顺序,但实际输出是一个行字符串。

您可以使用
$(“.table td”)
作为选择器来循环通过
td
并使用
text()
而不是
html()
来获取文本

$(“.table td”)。每个(函数(){
console.log($(this.text().trim());
});

六月十七日
七月十七日
8月17日
九月十七日
10月17日
11月17日
十二月十七日
一月十八日
二月十八日
三月十八日

使用
文本获取值,并使用
.table
而不是
#table

$(“.table td”)。每个(函数(){
var result=$(this.text().trim();
if(结果)console.log(结果);
});

六月十七日
七月十七日
8月17日
九月十七日
10月17日
11月17日
十二月十七日
一月十八日
二月十八日
三月十八日

您应该向表中添加
id
,以便调用它
#table
。您还可以使用
text()
函数获取span文本

$(document).ready(function(){
  $("table tr").each(function(){
    var result = $(this).find('span').text();
    //there are span elements that are empty, so i skip these ones
    if(result != ''){
        alert(result);
    }

  });
});

我很惊讶它会返回任何内容,因为您没有一个带有
id=“table”
的表。假设您的表选择器正确,它应该只返回
,即表中唯一
中第一个
的HTML内容,因此您的描述与代码不匹配。您的其余代码在哪里?你忘了你的
?堆栈溢出会让我把它全部发布@Phil@VictorOnyebuchi你回复错人了吗?谢谢!!非常感谢您的快速回复