<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> .shrink {font-size: 20px;} .emphasis {text-decoration: underline; color: red;} </style> </head> <body> <h1>Heading one</h1> <h1>Heading two</h1> <h1>Heading three</h1> <script type="text/javascript" src="jquery-1.8.0.min.js"></script> <script type="text/javascript" src="my_code.js"></script> </body> </html>
Sets the margin-left property. Example: css("margin-left", "50px") - Move the mouse over the <h1> elements to set the margin-left property.
$(document).ready(function() { $("h1").mouseenter(function() { $(this).css("margin-left", "50px"); }); });
Increments the margin-left property. Example: css("margin-left", "+=50px") - Move the mouse over the <h1> elements to increment the margin-left property.
$(document).ready(function() { $("h1").mouseenter(function() { $(this).css("margin-left", "+=50px"); }); });
Increments the margin-left property and sets the background-color property. Example: {"margin-left": "+=50px", "background-color": "red"} - Move the mouse over the <h1> elements to set the properties.
$(document).ready(function() { $("h1").mouseenter(function() { $(this).css({ "margin-left": "+=50px", "background-color": "red" }); }); });
Adds a class to an HTML element. Example: addClass("emphasis") - Click an <h1> element to add the emphasis class.
$(document).ready(function() { $("h1").click(function() { $(this).addClass("emphasis"); }); });
Adds multiple classes to an HTML element. Example: addClass("emphasis shrink") - Click an <h1> element to add the emphasis and shrink classes.
$(document).ready(function() { $("h1").click(function() { $(this).addClass("emphasis shrink"); }); });
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> .shrink {font-size: 20px;} .emphasis {text-decoration: underline; color: red;} </style> </head> <body> <h1 class="shrink emphasis">Heading one</h1> <h1>Heading two</h1> <h1>Heading three</h1> <script type="text/javascript" src="jquery-1.8.0.min.js"></script> <script type="text/javascript" src="my_code.js"></script> </body> </html>
Removes all classes from the specified HTML element. Example: removeClass() - Click the first <h1> element to remove all of its classes.
$(document).ready(function() { $("h1").click(function() { $(this).removeClass(); }); });
Removes a class from the specified HTML element. Example: removeClass("emphasis") - Click the first <h1> element to remove its emphasis class.
$(document).ready(function() { $("h1").click(function() { $(this).removeClass("emphasis"); }); });
Removes multiple classes from the specified HTML element. Example: removeClass("shrink emphasis") - Click the first <h1> element to remove its shrink and emphasis classes.
$(document).ready(function() { $("h1").click(function() { $(this).removeClass("shrink emphasis"); }); });
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> .shrink {font-size: 20px;} .emphasis {text-decoration: underline; color: red;} </style> </head> <body> <h1 class="emphasis">Heading one</h1> <h1>Heading two</h1> <h1>Heading three</h1> <script type="text/javascript" src="jquery-1.8.0.min.js"></script> <script type="text/javascript" src="my_code.js"></script> </body> </html>
removeClass() and addClass() can be used together. Example: $(this).removeClass("emphasis").addClass("shrink"); - Click the first <h1> element to remove its emphasis class and add the shrink class.
$(document).ready(function() { $("h1").click(function() { $(this).removeClass("emphasis").addClass("shrink"); }); });
Alternately removes and adds a class. Example: toggleClass("emphasis") - Repeatedly click an <h1> element to toggle the emphasis class.
$(document).ready(function() { $("h1").click(function() { $(this).toggleClass("emphasis"); }); });
Alternately removes and adds multiple classes. Example: toggleClass("shrink emphasis") - Repeatedly click an <h1> element to toggle the shrink and emphasis classes.
$(document).ready(function() { $("h1").click(function() { $(this).toggleClass("shrink emphasis"); }); });
Alternately removes and adds currently used classes. Example: toggleClass() - Repeatedly click the first <h1> element to toggle its emphasis class.
$(document).ready(function() { $("h1").click(function() { $(this).toggleClass(); }); });