Sri*_*san 2 html jquery html-table
我正在尝试在 html 表格中附加表格标题。
它的附加,但是它插入一个中断并在下一行附加。如何克服这一点?
请帮忙。提前致谢。在下面检查我的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<table id="test">
<thead>
<th> Column 1</th>
<th> Column 2</th>
<th> Column 3</th>
</thead>
</table>
<button> Click Me</button>
<script>
$('#test >thead').append('<th> Column 4</th>');
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
代码不起作用的原因:-
如果你看看渲染HTML你给代码的浏览器控制台,那么你会得到<tr></tr>被自动引入周围<th></th>,这就是为什么jQuery的附加<th> Column 4</th>的换行符。(一些足够聪明的浏览器会自动执行它们以使 HTML 结构正确)
您需要执行以下操作(tr自己添加HTML 并更改 jQuery):-
$('#test >thead tr').append('<th> Column 4</th>');
Run Code Online (Sandbox Code Playgroud)
工作示例:-
$('#test >thead tr').append('<th> Column 4</th>');
Run Code Online (Sandbox Code Playgroud)
$('#test >thead tr').append('<th> Column 4</th>');Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8350 次 |
| 最近记录: |