使用JQuery将一些文本附加到每个表头(th)

时间:2017-09-13 12:25:35

标签: javascript jquery html-table append

我想在每个内部添加一些附加内容。表头是动态生成的,取决于数据库列

以下是我所拥有的:

<table id="dynamic_id" class="table table-striped  table-bordered">
    <thead>
        <tr>
            <th>Company Name</th>
            <th>Email</th>
            <th>Mobile</th>
            <th>Address</th>
        </tr>

    </thead>
    <tbody>
         //body goes here
    </tbody>

    </tbody>
</table>

什么是期望在Ready Event上使用jQuery。

 <th>Company Name   
           <span class="js-sorter-desc fa fa-chevron-down pull-right"></span> 
           <span class="js-sorter-asc  fa fa-chevron-up pull-right"></span>
 </th>

这应该适用于每一个......

<span class="js-sorter-desc fa fa-chevron-down pull-right"></span>
<span class="js-sorter-asc  fa fa-chevron-up pull-right">

2 个答案:

答案 0 :(得分:1)

您必须使用append jquery 方法。

另外,使用find方法获取所有th DOM 元素。

&#13;
&#13;
$('.table.table-striped').find('thead tr th').append('<span class="js-sorter-desc fa fa-chevron-down pull-right"></span><span class="js-sorter-asc  fa fa-chevron-up pull-right"></span>');
console.log($('.table.table-striped').html());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="dynamic_id" class="table table-striped  table-bordered">
    <thead>
        <tr>
            <th>Company Name</th>
            <th>Email</th>
            <th>Mobile</th>
            <th>Address</th>
        </tr>

    </thead>
    <tbody>
         //body goes here
    </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个:

$(document).ready(function(){
    $('table').find('th')append('<span class="js-sorter-desc fa fa-chevron-down pull-right"></span><span class="js-sorter-asc  fa fa-chevron-up pull-right">');
});