我得到了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()。 –