如何在jquery中更改表格的文本?

时间:2015-03-24 05:43:24

标签: jquery html5 css3 jquery-ui

我有如下表格

<table>
 <tr> <th>Document</th> <th>...</th></tr>
 <tr><td>...</td>... </tr>
 ...
</table>

现在我想通过jQuery将“文档”的文本更改为“营销文档/ URL”...请帮助我!!!

7 个答案:

答案 0 :(得分:5)

$("table tr>th:first").html("Marketting Document/URL");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
 <tr> <th>Document</th> <th>1</th></tr>
 <tr><td>2</td>3 </tr>
</table>

尝试这样的事情:

console.log($("table tr>th:first").html("Marketting Document/URL"));

答案 1 :(得分:3)

您可以尝试这样做:使用jQuery选择器获取第一个tr,然后在其中输入th。然后使用.text().html()方法更新所需的值。

$(function(){
  $('table tr:first th:first').text('Marketting Document/URL');
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
  <tr>
    <th>Document</th>
    <th>second header</th>
  </tr>
  <tr>
    <td>first td</td>
    <td>second td</td>
  </tr>
</table>

答案 2 :(得分:1)

将“id”赋予“th”

 <table>
 <tr> <th id="thtext">Document</th> <th></th></tr>
 <tr><td></td> </tr>

然后在javascript函数中添加此

 $("#thtext").html("Marketting Document/URL");

答案 3 :(得分:0)

尝试这样first() Demo Here

$(function(){
  $('table tr>th').first().html('Marketting Document/URL');
});

答案 4 :(得分:0)

//第一个为第一个tr

$(function(){
  $('table tr:first').find('th:first').html('Marketting Document/URL');
});

//对于所有tr的第一个

$(function(){
  $('table tr').find('th:first').html('Marketting Document/URL');
});

答案 5 :(得分:0)

您可以在表格标题中搜索包含“文档”的文本,如下面的代码,并将值替换为新值。

<script>
 $(function(){
   var m = $('table tr th:contains("Document")');
       m.text('Marketting Document/URL');
 });
</script>

答案 6 :(得分:0)

要添加到其他解决方案中,您可能还希望跳过第一行(通常为 header 行)。为此,我们可以这样处理:

$(function(){
    $('table tr').not(':first').find('th:first').html('Some Text');
});

一个实际的用例是在行标题中插入号(在 spreadsheet 应用程序中可以找到),其中我们可以遍历每一行并添加循环index作为号(image)。