【发布时间】:2021-04-26 09:09:00
【问题描述】:
https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_html_after2
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function afterText() {
$("#tblGrid").html("<td>NameName2</td><td>Budget Value 2</td></tr><tr><td>NameName3</td><td>Budget Value 3</td>");
}
</script>
</head>
<body>
<table>
<tbody>
<th><h4>Name Heading</h4></th>
<th>
<table>
<thead>
<tr>
<th>Budget Heading</th>
</tr>
</thead>
</table>
</th>
<tr id="tblGrid"></tr>
<tr><td>NameName1</td><td>Budget Value 1</td></tr>
</tbody>
</table>
<button onclick="afterText()">Button</button>
</body>
</html>
点击按钮后输出是这样的:
名称标题预算标题
NameName3 预算值 3
NameName1 预算值 1
按钮
您能否帮助了解数字“2”的价值在哪里?
预期输出:
名称标题预算标题
NameName2 预算值 2
NameName3 预算值 3
NameName1 预算值 1
按钮
而且每次我们点击按钮时,它不应该追加 2 和 3,而是清除旧的迭代值。请提出建议!
【问题讨论】:
-
您正在尝试在现有的 tr 元素中添加
</tr>...这是无效的 HTML...也许浏览器正在丢弃它之前的所有内容。
标签: html jquery html-table jquery-selectors