現在のセレクターですべての項目を選択するにはどうすればよいですか? 少し怠惰に聞こえるかもしれませんが、クラス名を使用せずに、これが可能かどうかを知りたいです。
$(document).on('click', '.selector', function() {
// Select all nodes based on this selector
$($(this).context) // Tried this but didn't work. I want to use $(this) to get the original selector (.selector), but not use $(this).className
});
したがって、書く代わりに:
$('.selector').removeClass('selected');
$(this).addClass('selected');
私が欲しい:
$($(this).getSelector()).removeClass('selected'); // Remove class from all nodes with this selector
$(this).addClass('selected'); // Only add class to the item currently clicked
これは、セレクターを繰り返さないためです。