我在页面上有40个foo,每个都在另一个函数中分配了一个左/上值. $('.foo').css({ top: isOn ? current_top_value_here : 500, left: isOn ? current_left_value_here : 500}); 所以,我想离开顶部如果isOn为true,则保留未
$('.foo').css({
top: isOn ? current_top_value_here : 500,
left: isOn ? current_left_value_here : 500
});
所以,我想离开顶部&如果isOn为true,则保留未修改的左侧属性.我怎样才能做到这一点?
这样您就可以确保只在需要时才添加它们.首先使用您想要分配的值设置对象,并有条件地添加更多.然后你可以将对象传递给.css().var cssProperties={
'color' : 'yellow',
...
};
if (isOn) {
cssProperties.top=500;
cssProperties.left=500;
/* alternative: $.extend(cssProperties, { 'top': 500, 'left': 500 }); */
}
$('.foo').css(cssProperties);
