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 ? 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.
You cannot include a div
tag as a direct child of a table
- use a tbody
for this purpose intead:
<table width="500" border="1" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" align="center">
<thead>
<tr>
...
</tr>
</thead>
<tbody id='selector' >
...
</tbody>
</table>
精彩评论