Rails 3.1を実行していて、ファイル/app/assets/javascripts/procedures.jsに次のjqueryスクリプトがあります。
$(document).ready(function(){
$('.unselected').click(function() {
$(this).removeClass('unselected');
$(this).addClass('selected');
});
$('.selected').click(function() {
$(this).removeClass('selected');
$(this).addClass('unselected');
});
});
最初のアクションは期待どおりに機能します(選択)が、選択解除は機能しません。
一方、このスクリプトは期待どおりに機能します。
$(document).ready(function(){
$('.tile').click(function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
$(this).addClass('unselected');
} else if ($(this).hasClass('unselected')) {
$(this).removeClass('unselected');
$(this).addClass('selected');
}
});
});
イベントリスナーは、スクリプトが最初に実行されたときにのみ割り当てられるためですか?