gpt4 book ai didi

javascript - 如何在通过 jquery 向表中插入一行后保持 css 样式

转载 作者:行者123 更新时间:2023-11-27 23:27:58 26 4
gpt4 key购买 nike

我有一个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 样式消失。

result

为什么 css 对添加行不起作用以及如何保持添加新行的 css 样式?非常感谢!

最佳答案

这是我的答案,

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

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<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">button</button>

关于javascript - 如何在通过 jquery 向表中插入一行后保持 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com