[英]Jquery append a div area
Hi I want to add a new line in a table every time a button is clicked in my code, The code below works but I am expecting it inside the DIV tag called 'selector' as an addition to my table but instead it appears at the top of the page, What am i doing wrong please ? 嗨,我想在我的代码中每次单击按钮时在表中添加新行,下面的代码可以工作,但是我希望它在DIV标签(称为“选择器”)中作为对我表的补充,但它会出现在页面顶部,请问我在做什么错? thanks
谢谢
<script type="text/javascript">
$(document).ready(function(){
$("#test").click(function() { tested(this) });
});
function tested() {
newline = "<tr bgcolor='#666666'><td> </td> <td><input type='button' id='test1' value='Click to Test' /></td><td> </td> </tr> " ;
$('#selector').append(newline)
}
</script>
<table width="500" border="1" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" align="center">
<tr>
<td width="50">top</td>
<td> </td>
<td width="50"> </td>
</tr>
<div id='selector' >
<tr bgcolor="#666666">
<td> </td>
<td><input type="button" id="test" value="Click to Test" /></td>
<td> </td>
</tr>
</div>
<tr>
<td>Bottom</td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
Your code is not valid html - and this is bound to cause unexpected behaviour in different browsers. 您的代码无效的html-势必在不同的浏览器中导致意外行为。
You cannot include a div
tag as a direct child of a table
- use a tbody
for this purpose intead: 您不能将
div
标记作为table
的直接子项包含-为此,请使用tbody
:
<table width="500" border="1" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" align="center">
<thead>
<tr>
...
</tr>
</thead>
<tbody id='selector' >
...
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.