jquery addClass() method


  Translation results:

add

英[æd] 美[æd]

vt.Add; add; add explanation; include…

vi. Increase; do addition; accumulate; expand

n. Addition, addition operation; (the supplementary part of a report)

class

英[klɑ:s] 美[klæs]

n.Class; class; level; category

vt. Classify... into a certain level, regard... as (or classification, Classify); put... into a certain class

adj. Very good, excellent, outstanding

vi. Belong to... category (or level), be listed as a certain category ( or a certain level)

jquery addClass() methodsyntax

Function: addClass() method adds one or more classes to the selected element. This method does not remove existing class attributes, it only adds one or more class attributes. If you need to add multiple classes, separate the class names with spaces.

Syntax: $(selector).addClass(class)

##Parameters:

ParameterDescriptionclass Required. Specifies one or more class names.

Syntax: $(selector).addClass(function(index,oldclass))

Parameters :

ParameterDescription##function(index,oldclass) index class

jquery addClass() 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:first").addClass("intro");
  });
});
</script>
<style type="text/css">
.intro
{
font-size:120%;
color:red;
}
</style>
</head>

<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>向第一个 p 元素添加一个类</button>
</body>
</html>
Run instance »

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

Required. Specifies a function that returns one or more class names to be added.
Optional. The index position of the selector.
Optional. The old class name of the selector.