Manipular css
Click here to load reader
-
Upload
cristiano-borges -
Category
Documents
-
view
218 -
download
0
Transcript of Manipular css
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 1/6
addClass()
Categories: Attributes | Manipulation > Class Attribute | CSS
.addClass( className )Returns: jQuery
Description: Adds the specified class(es) to each element in the set of
matched elements.
version added: 1.0 .addClass( className )
o className
Type:String
One or more space-separated classes to be added to the class attribute of eachmatched element.
version added: 1.4 .addClass( function )
o function
Type:Function( Integer index,String currentClassName ) =>String
A function returning one or more space-separated class names to be added to the
existing class name(s). Receives the index position of the element in the set and
the existing class name(s) as arguments. Within the function,this refers to the
current element in the set.
It's important to note that this method does not replace a class. It simply adds the
class, appending it to any which may already be assigned to the elements.
More than one class may be added at a time, separated by a space, to the set of
matched elements, like so:
1 $( "p" ).addClass( "myClass yourClass" );
This method is often used with.removeClass() to switch elements' classes fromone to another, like so:
1 $( "p" ).removeClass( "myClass noClass" ).addClass( "yourClass" );
Here, themyClass andnoClass classes are removed from all paragraphs,
whileyourClass is added.
As of jQuery 1.4, the.addClass() method's argument can receive a function.
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 2/6
1
2
3
$( "ul li" ).addClass(function( index ) {
return "item-" + index;
});
Given an unordered list with two<li> elements, this example adds the class "item-
0" to the first<li> and "item-1" to the second.
Examples:
Example: Add the class "selected" to the matched elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!doctype html>
<html lan!"en">
<head>
<meta harset!"ut#-">
<title>addClass demo<%title>
<style>
p {
marin& px;
#ont-si'e& px;
}
.selected {
olor& *lue;
}
.highlight {
*around& yello,;
}
<%style>
<sript sr!"https&%%ode.uery.om%uery-./.0.s" ><%sript>
<%head>
<*ody>
<p>1ello<%p>
<p>and<%p>
<p>2ood*ye<%p>
<sript>
$( "p" ).last().addClass( "seleted" );
<%sript>
<%*ody>
<%html>
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 3/6
19
20
21
22
23
24
25
26
27
28
29
30
31
Demo:
Example: Add the classes "selected" and "highlight" to the matched
elements.
1
2
3
4
5
6
7
8
9
<!doctype html>
<html lan!"en">
<head>
<meta harset!"ut#-">
<title>addClass demo<%title>
<style>
p {
marin& px; #ont-si'e& px;
}
.selected {
olor& red;
}
.highlight {
*around& yello,;
}
<%style>
<sript sr!"https&%%ode.uery.om%uery-./.0.s" ><%sript>
<%head>
<*ody>
<p>1ello<%p>
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 4/6
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<p>and<%p>
<p>2ood*ye<%p>
<sript>
$( "p&last" ).addClass( "seleted hihliht" );
<%sript>
<%*ody>
<%html>
Demo:
Example:Pass in a function to.addClass() to add the "green" class to a
div that already has a "red" class.
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 5/6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!doctype html>
<html lan!"en">
<head>
<meta harset!"ut#-">
<title>addClass demo<%title>
<style>
div { *around& ,hite;
}
.red {
*around& red;
}
.red.green {
*around& reen;
}
<%style>
<sript sr!"https&%%ode.uery.om%uery-./.0.s" ><%sript>
<%head>
<*ody>
<div>3his div should *e ,hite<%div>
<div lass!"red">3his div ,ill *e reen *eause it no, has the "reen" an
4t ,ould *e red i# the addClass #untion #ailed.<%div>
<div>3his div should *e ,hite<%div>
<p>3here are 'ero reen divs<%p>
<sript>
$( "div" ).addClass(function( index5 urrentClass ) {
var addedClass;
if ( urrentClass !!! "red" ) {
addedClass ! "reen";
$( "p" ).text( "3here is one reen div" );
}
return addedClass;
});
<%sript>
<%*ody>
<%html>
8/9/2019 Manipular css
http://slidepdf.com/reader/full/manipular-css 6/6
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Demo: