首頁(yè)  >  CSS  > css(name|pro|[,val|fn])

返回值:Stringcss(name|pro|[,val|fn])

jQuery css() 方法概述

訪(fǎng)問(wèn)匹配元素的樣式屬性。

jQuery 1.8中,當(dāng)你使用CSS屬性在css()animate()中,我們將根據(jù)瀏覽器自動(dòng)加上前綴(在適當(dāng)?shù)臅r(shí)候),比如("user-select", "none"); 在Chrome/Safari瀏覽器中我們將設(shè)置為"-webkit-user-select", Firefox會(huì)使用"-moz-user-select", IE10將使用"-ms-user-select".

參數(shù)

nameStringV1.0

要訪(fǎng)問(wèn)的屬性名稱(chēng)

nameArrayV1.9

一個(gè)或多個(gè)CSS屬性組成的一個(gè)數(shù)組

propertiesMapV1.0

要設(shè)置為樣式屬性的名/值對(duì)

name,valueString, NumberV1.4

屬性名,屬性值

name,function(index, value)String,FunctionV1.0

1:屬性名

2:此函數(shù)返回要設(shè)置的屬性值。接受兩個(gè)參數(shù),index為元素在對(duì)象集合中的索引位置,value是原先的屬性值。

示例

參數(shù)name 描述:

取得第一個(gè)段落的color樣式屬性的值。

jQuery 代碼:
$("p").css("color");

參數(shù)properties 描述:

將所有段落的字體顏色設(shè)為紅色并且背景為藍(lán)色。

jQuery 代碼:
$("p").css({ "color": "#ff0011", "background": "blue" });

參數(shù)name,value 描述:

將所有段落字體設(shè)為紅色

jQuery 代碼:
$("p").css("color","red");

參數(shù)name,回調(diào)函數(shù) 描述:

逐漸增加div的大小

jQuery 代碼:
  $("div").click(function() {
    $(this).css({
      width: function(index, value) {
        return parseFloat(value) * 1.2;
      }, 
      height: function(index, value) {
        return parseFloat(value) * 1.2;
      }
    });
  });