我需要將一個元素傳遞給一個函數,然後在遍歷父項時匹配該特定元素。捕捉(對於像我這樣不懂的人)是因爲這個元素沒有id。在下面的例子中,我希望每一個元素變成粉紅色,除了一個點擊應該變黃jQuery - 如何檢查兩個元素是否相同?
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));
});
見http://stackoverflow.com/questions/4441135/how-to-compare-two-doms-or-dom-nodes-in-general –