jQuery基础篇之CSS操作

 

  1、css(name)    

 

  • 访问第一个匹配元素的样式属性
  • name参数是要访问的属性名称


/*
获取第一个span的width的值
*/
$('span').css('width');


 2、 css(properties)



  • 把一个"名|值"对象设置为所有匹配元素的样式属性
  • properties参数是要设置样式属性的名|值对


/*
属性名包含"-"的话,必须加引号
*/
$('span').css({"margin-left":"20px"})







 



3、 css(name,value)



  • 所有匹配的元素中,设置一个样式属性的值
  • name参数是属性名
  • value参数是属性值


/*数字会自动转化为像素*/
$('span').css("width","50");


4、 css(name,function(index,value))



  • 所有匹配的元素中,设置一个样式属性的值
  • name参数是属性名
  • function(index,value) 参数是返回要设置的属性值--index是索引 | value是原先属性的值

$('div').click(function(){
    $(this).css(width,function(index,value){
          return parseFloat(value)*1.4;
   });
});