我需要将一个元素传递给一个函数,并在遍历其父元素时匹配该特定元素。对于像我这样无知的人来说,问题在于该元素没有id属性。在下面的示例中,我想让每个元素变成粉色,除了被单击的那个应该变成黄色。
function colorize(element) {
element.parent().find('span').each(function() {
if ($(this)===element) { // the problem is this is always false
$(this).css('background','yellow');
} else {
$(this).css('background','pink');
}
});
}
$('span').click(function() {
colorize($(this));
});