用的jquery 為什么能動(dòng)態(tài)添加 不能動(dòng)態(tài)刪除?
動(dòng)態(tài)添加的行不能刪除,原來(lái)的行可以刪除
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ??
? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
??
?</head>?
?<body>?
? <table border="3" width="50%" id="table">
? <tr>
<th>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
? <tr>
<td>xh001</td>
<td>王小明</td>
<td><a>刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td class='sd' ><a>刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input ?type="button" value="添加一行" onclick="add()" /> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
? ? ? ?<script type="text/javascript">?
? ? ? ? var div1 ="<tr><td>xh002</td><td>劉小芳</td><td><a>刪除</a></td></tr>"
? ? ? ? $('input').click(function(){
? ? ? ? ? ? //alert("dsfsfd")?
? ? ? ? ? ? $('table').append(div1);
? ? ? ? })
? ? ? ? $("a").on('click',function(){
? ? ? ? ? ? $(this).parents('tr').remove();
? ? ? ? })
? ? ??
? </script>
?</body>
</html>
2016-10-13
新添加的行,a標(biāo)簽沒(méi)有并沒(méi)有添加事件,
$('input').click(function() {
//alert("dsfsfd")?
$('table').append(div1);
$("a").on('click', function() {
$(this).parents('tr').remove();
})
})
$("a").on('click', function() {
$(this).parents('tr').remove();
})