replaceWith()被替換過后的元素可以重新在綁定事件嗎?
使用replaceWith()被替換過后的元素可以重新在綁定事件嗎?我嘗試了一下,有的時候可以被點擊,但是大部分的情況下,還是不能被點擊的,為什么會這樣呢?代碼如下<br/>
<button>點擊的時候</button>
<div>被替換的內容</div>
<p>需要變動的地方</p>
<script>
$(function(){
$("button").bind("click",function(){
$("span").click(function(){
$("p").css({"color":"blue"})
});
$("div").replaceWith("<br/><span class='nnnn'>哎呀,我竟然被替換了</span>");
});
$("div").click(function(){
$("p").css({"color":"red"})
});
})
</script>
2016-04-14
2016-04-12
應該是你要的效果吧,點擊被span替換之后的元素之后,p標簽的顏色被替換為紅色。