如何通过jQuery向表中插入行后保持css样式

我有一个CSS风格

    #mytbody > tr > th {
        background-color: red;
    }

和脚本

    function myclick() {
        $("#mytbody").append("<tr><td>1</td><td>1</td>/tr>");
    }

这是我的html代码:

<table>
    <thead>
        <tr>
            <td>head 1</td>
            <td>head 2</td>
        </tr>
    </thead>
    <tbody id="mytbody">
        <tr>
            <th>1</th>
            <th>2</th>
        </tr>
    </tbody>
</table>
<button type="button" onclick="myclick()">button</button>

单击按钮添加的新行的css样式消失了。

结果

为什么CSS不能用于添加行,以及如何保持CSS样式来添加新行?非常感谢你!

穆克什·拉姆(Mukesh Ram)

这是因为您的CSS仅用于th标记。您还应该添加CSS td

#mytbody > tr > th,
#mytbody > tr > td {
    background-color: red;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章