您好,我正在尝试使用 jQuery 向表中添加一行,但它不起作用。
可能是什么原因?
而且,我可以为新添加的行添加一些值吗?
代码如下:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$('a').click(function() {
$('#myTable').childs('tr').append('<tr class="child"><td>blahblah<\/td></tr>');
});
</script>
<title></title>
</head>
<body>
<a href="">Link</a>
<table id="myTable">
<tbody>
<tr>
<td>
test
</td>
</tr>
</tbody>
</table>
</body>
</html>
最佳答案
我假设您想将此行添加到 <tbody>
元素,只需使用 append()
在 <table>
上将插入 <tr>
外面<tbody>
,可能会产生不良结果。
$('a').click(function() {
$('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
});
编辑:这是完整的源代码,它确实有效:(注意 $(document).ready(function(){});
,之前不存在。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function() {
$('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
});
});
</script>
<title></title>
</head>
<body>
<a href="javascript:void(0);">Link</a>
<table id="myTable">
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</body>
</html>
关于jquery - 如何使用 jQuery 将行追加到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160890/