[英]How do I bind to the click event from within the click event when I need to do it repeatedly?
我有代码,所以当你点击一个单词时,它会被另一个单词替换。
<script>
$(document).ready(function() {
$('.note_text').click(function(){
$(this).remove();
$('#note_div').append('<span class="note_text">new</span>');
// re-applying behaviour code here
});
});
</script>
<div id="note_div">
<span class="note_text">preparing</span>
</div>
我需要附加的单词具有相同的点击行为。 做这个的最好方式是什么?
更改
$('.note_text').click(function(){
至
$('.note_text').live('click',function(){
这将导致页面上任何类型的'note_text'具有.live设置的行为
您应该为此目的使用.live()
帮助或.delegate()
帮助绑定。
$(function() {
$('#note_div').delegate('.note_text', 'click', function(e) {
$(e.target).parent().append("<span class='note_text'>new</span>").end().remove();
});
});
演示 : http : //www.jsfiddle.net/PkngP/2/
你可以重新绑定处理程序:
function handler(){
$(this).remove();
$('#note_div').append("<span class="note_text">new</span>");
$(".note_text").unbind("click");
$('.note_text').click(handler);
}
$(document).ready(function() {
$('.note_text').click(handler);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.