如何获取<th>标签的文本



这似乎应该很简单,但我无法正常工作。

html

<table id="mytable">
   <thead>
      <tr>
         <th>Head1</th>
         <th>Head2</th>
      </tr>
   </thead>
   <tbody>
      ..body stuff..
   </tbody>
</table>

JS

$("#mytable > thead > tr > th").each(function () {
   console.log($(this).text();
})

控制台显示每个文本值的"未定义"。

请注意控制台..您有一个语法错误 ($(this).text()应该是 ($(this).text())你忘记了 )

$("#mytable > thead > tr > th").each(function () {
   console.log($(this).text());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
   <thead>
      <tr>
         <th>Head1</th>
         <th>Head2</th>
      </tr>
   </thead>
   <tbody>
      ..body stuff..
   </tbody>
</table>

重要:确保包括jQuery

相关内容

最新更新