我得到了jQuery的加快和woudl想知道我怎麼能寫出下面的代碼,以便它更幹,使用$(本)使用jQuery「本」與「選擇」
//Update Background Color
(function($) {
//Find Background Colour
var curColor = $('ul.product-categories li.current-item a').css('color');
if($('ul.product-categories li').hasClass('current-item')) {
//trying to use 'this' below but does not work
//$(this).children('a').css({
//This does work
$('ul.product-categories li.current-item a').css({
'background-color': curColor,
'color': '#fff'
});
} else {
$('ul.product-categories li.cat-item-11 a').css({
'background-color': '#dd3c85',
'color': '#fff'
});
};
})(jQuery);
位
關於如何改進的任何建議。我知道這可能是一些簡單的...
感謝
沒有理由在該代碼中使用$(this)。你應該使用'curColor' – epascarello
爲什麼不把$('ul.product-categories li')設置爲一個變量,然後在需要某些元素時在下面的變量上使用.find()。 –