jquery css() method


  Translation results:

css

abbr.control-stick steering joystick control;combat service support combat service support;coded switch system password transfer system;cryogenic storage system cryogenic storage system

jquery css() methodsyntax

Function: css() method returns or sets one or more style attributes of the matching element.

Return css properties: Return the CSS property value of the first matching element. Shorthand CSS properties (such as "background" and "border") are not supported when used to return a value.

Syntax: $(selector).css(name)'

Parameters:

ParametersDescription
name Required. Specifies the name of the CSS property. This parameter can contain any CSS property. For example "color".

Set css properties: Set the specified CSS properties for all matching elements.

Syntax: $(selector).css(name,value)

Parameters:

ParametersDescription
name Required. Specifies the name of the CSS property. This parameter can contain any CSS property, such as "color".
value Optional. Specifies the value of a CSS property. This parameter can contain any CSS property value, such as "red". If the empty string value is set, removes the specified attribute from the element.

Use functions to set css attributes: Set the value of the style attribute in all matching elements. This function returns the property value to be set. Accepts two parameters, index is the index position of the element in the object collection, and value is the original attribute value.

Syntax: $(selector).css(name,function(index,value))

Parameters:

ParametersDescription
name Required. Specifies the name of the CSS property. This parameter can contain any CSS property, such as "color".
function(index,value)Specifies a function that returns the new value of a CSS property.
index Optional. Accepts the index position of the selector
oldvalue Optional. Accepts the current value of a CSS property.

Set multiple css attributes/values: Set the "name/value pair" object as the style attribute of all matching elements. This is the best way to set a large number of style properties on all matching elements.

Syntax: $(selector).css({property:value, property:value, ...})

Parameters:

Parameter Description
{property:value} Required. Specifies the name/value pair object to be set as a style attribute. This parameter can contain several CSS property name/value pairs. For example {"color":"red","font-weight":"bold"}

jquery css() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("color","red");
  });
});
</script>
</head>

<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">改变段落的颜色</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance

Popular Recommendations

Home

Videos

Q&A