freeCodeCamp/guide/chinese/jquery/jquery-css-method/index.md

58 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: CSS Method
localeTitle: CSS方法
---
## CSS方法
jQuery `.css()`方法获取匹配元素集中第一个元素的计算样式属性的值或为每个匹配元素设置一个或多个CSS属性。
### 入门
要返回指定CSS属性的值请使用以下语法
```js
$(selector).css(propertyName);
```
例如:
```js
$('#element').css('background');
```
注意这里我们可以使用任何css选择器例如elementHTML Tag selector。elementClass SelectorelementID selector
### 设置
要设置指定的CSS属性请使用以下语法
```js
$(selector).css(propertyName,value);
```
例如:
```js
$('#element').css('background','red');
```
要设置多个CSS属性您必须使用对象文字语法如下所示
```js
$('#element').css({
'background': 'gray',
'color': 'white'
});
```
如果要更改标有多个单词的属性,请参阅此示例:
更改元素的`background-color`
```js
$('#element').css('background-color', 'gray');
```
#### 更多信息:
文档: [api.jquery](http://api.jquery.com/css/)