如何使用当前选择器选择所有项目?可能听起来有点懒惰,但我想知道这是否可能——而不是使用类名。
$(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
这是为了不重复选择器。