最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to dynamically add th to the existing th using Jquery - Stack Overflow

programmeradmin5浏览0评论

How can i add value to th dynamically using Jquery. I am looking to append the values to th Heading8 and Heading9 after Heading7

<thead>
    <tr>
        <th>Heading1</th>
        <th>Heading2</th>
        <th>Heading3</th>
        <th>Heading4</th>
        <th>Heading5</th>
        <th>Heading6</th>
        <th>Heading7</th>           
    </tr>
</thead>

How can i add value to th dynamically using Jquery. I am looking to append the values to th Heading8 and Heading9 after Heading7

<thead>
    <tr>
        <th>Heading1</th>
        <th>Heading2</th>
        <th>Heading3</th>
        <th>Heading4</th>
        <th>Heading5</th>
        <th>Heading6</th>
        <th>Heading7</th>           
    </tr>
</thead>
Share asked Sep 17, 2014 at 16:37 user3541857user3541857 292 silver badges8 bronze badges 3
  • 3 $('thead tr').append( $('<th />', {text : 'Heading8'}) ) – adeneo Commented Sep 17, 2014 at 16:39
  • If there re many tables in a page , and If I have to append this only to a specific table, then how can i achieve my requirement. – user3541857 Commented Sep 17, 2014 at 16:58
  • Give the table an ID – adeneo Commented Sep 17, 2014 at 17:56
Add a ment  | 

3 Answers 3

Reset to default 2

Use jQuery append:

//adds new <th> Heading8 after Heading7
$('thead tr').append( $('<th />', {text : 'Heading8'}) )

//adds new <th> Heading9 after Heading8
$('thead tr').append( $('<th />', {text : 'Heading9'}) )

EDIT

Append to a specific table:

<thead>
  <tr id="selectMe">
    <th>Heading1</th>
    <th>Heading2</th>         
  </tr>
</thead>

Then use:

$('#selectMe').append( $('<th />', {text : 'Heading3'}) )

Just add this to jQuery:

$(document).ready( function() {
    $('table thead th:last-child').after('<th>test</th>');
});

Use the following mand:

$('#tableId').append($('<th />', {text : 'Your Text'}))
发布评论

评论列表(0)

  1. 暂无评论