はい、もう一度呼び出すとイベントハンドラーが複製されますが、最初のリスナーがトリガーされた後にクラスが見つからない場合に何もしないメソッドをobserveRemoveRoom
呼び出しているだけなので、目立たないかもしれません。removeClass
代わりに、次のように、毎回クリック イベントのバインドを解除して再バインドできます。
var observeRemoveRoom = function(){
var remove_class = function(){
$(this).parent().removeClass('active');
};
$('.remove_room').off('click', remove_class).on('click', remove_class);
}
ただし、次のように、毎回イベントをバインドおよびバインド解除するのではなく、この関数の外でこれを行うことをお勧めします。
$(document).ready(function(){
var remove_class = function(){
$(this).parent().removeClass('active');
};
// If the element exists at dom ready, you can bind the event directly
$('.remove_room').on("click", remove_class);
// If the element is added in dynamically, you can [delegate][1] the event
$('body').on("click", '.remove_room', remove_class);
// Note: Although I've delegated the event to the body tag in this case
// I recommend that you use the closest available parent instead
});
http://api.jquery.com/on/#direct-and-delegated-events : [1]