<html>
<head>
<meta charset="utf8">
<title>jquery动态添加的元素为何不能删除</title>
<script type="text/javascript" src="jquery-1.11.2.min.js"></script>
<style type="text/css">
.row{width:200px;height:500px;background: yellow;}
.one{width:200px;height:50px;background: green;}
</style>
</head>
<body>
<button id='add'>新增元素</button>
<div class="row">
<div class="one">
<span>删除</span>
</div>
<div class="one">
<span>删除</span>
</div>
</div>
</body>
<script type="text/javascript">
var str ="<div class='one' style='font-weight:bold;' >新添加的元素 <sapn>删除</span></div>";
$(document).ready(function(){
$("button").click(function(){
$(".row").append(str);
});
});
$('span').click(function(){
$(this).parent().remove();
})
</script>
</html>
é¦å ï¼ä½ çæ ç¾éäº<sapn>å é¤</span>ï¼åºè¯¥æ¯spanï¼ä½ åéäºã
å ¶æ¬¡ï¼å¨æå è½½çåºè¯¥ç¨ç»å®äºä»¶ãæ ¹æ®ä½ å¼å ¥çjqåºé£å°±è¦ç¨.on()äºä»¶ãå ·ä½ä»£ç ï¼
$("span").on("click",function(){æ ç¾éäºï¼å¯¹ä¸èµ·ãä½æ¯ä½ è¿ä¸ªä»£ç ææ¾è¿å»äºï¼ä¸è¡åãç¹å é¤è¿æ²¡ååºåï¼ï¼ï¼ï¼
追ç$("span").on("click",function(){æè¿è¾¹è¿æ¯ä¸è¡ï¼ä½ é£è¾¹è¿è¡è¯äºåï¼
追ç大å¥ï¼è½ä¸è½æè¿ä¸ªæ¾å°$(function(){})éé¢ï¼æ¾å°å¤é¢è°è®¤è¯ï¼
为什么
$('span').click(function(){
$(this).parent().remove();
})
我这样写不行?啥原因?
怎么绑定?我不会写。。
追答jQuery动态添加元素后,为什么不能动态删除?
jquery动态添加的元素为何不能删除
.row{width:200px;height:500px;background: yellow;}
.one{width:200px;height:50px;background: green;}
新增元素
删除
删除
var str ="新添加的元素 删除";
$(document).ready(function(){
$("button").click(function(){
$(".row").append(str);
$('span').click(function(){
$(this
});
});
$('span').click(function(){
$(this).parent().remove();
})
错了……手机编辑不方便,明天发给你吧
我试了,代替掉remove不行。。