Jquery追加一个div区域
您好,我想在表格中添加一个新行,每次在我的代码中单击按钮时,下面的代码都可以工作,但我希望它位于名为“选择器”的DIV标记中,作为我表格的一个添加项,但它却出现在页面顶部,请问我做错了什么?谢谢Jquery追加一个div区域,jquery,Jquery,您好,我想在表格中添加一个新行,每次在我的代码中单击按钮时,下面的代码都可以工作,但我希望它位于名为“选择器”的DIV标记中,作为我表格的一个添加项,但它却出现在页面顶部,请问我做错了什么?谢谢 <script type="text/javascript"> $(document).ready(function(){ $("#test").click(function() { tested(this) });
<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>
$(文档).ready(函数(){
$(“#测试”)。单击(function(){tested(this)});
});
功能测试(){
新行=”;
$(“#选择器”).append(换行符)
}
顶部
底部
您的代码不是有效的html-这必然会在不同的浏览器中导致意外行为
不能将div
标记包含为表的直接子项
-为此,请使用tbody
:
<table width="500" border="1" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" align="center">
<thead>
<tr>
...
</tr>
</thead>
<tbody id='selector' >
...
</tbody>
</table>
...
...
您的代码不是有效的html-这必然会在不同的浏览器中导致意外行为
不能将div
标记包含为表的直接子项
-为此,请使用tbody
:
<table width="500" border="1" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" align="center">
<thead>
<tr>
...
</tr>
</thead>
<tbody id='selector' >
...
</tbody>
</table>
...
...
为什么要将其传递给函数调用,但没有参数。。。您应该在tested中使用一个参数,并在jQuery选择器中使用它。为什么要将它传递给函数调用,但没有参数。。。您应该在tested中使用一个参数,并在jQuery选择器中使用该参数。谢谢Dexter,我将复习一下我的html!谢谢Dexter,我会复习我的html!