繁体   English   中英

当我需要重复执行时,如何从click事件中绑定click事件?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM