Setting method: 1. Use the css() method, the syntax "$(selector).css("Attribute name", "Attribute value")"; 2. Use the attr() method, the syntax "$( selector).attr("style","Attribute name 1: attribute value 1; attribute name 2: attribute value 3;...")".
The operating environment of this tutorial: windows7 system, jquery1.7.2 version, Dell G3 computer.
Method 1: Use the css() method
The css() method can set one or more style attributes of the matching element. Syntax:
$(selector).css("属性名", "属性值")
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> </head> <body> <div id="dome">欢迎来到PHP中文网!</div> <script type="text/javascript"> $("#dome").css("color", "red"); </script> </body> </html>
Rendering:
Method 2: Use the attr() method
attr() method sets or returns the attribute value of the selected element. Grammar:
$(selector).attr("style","属性名1:属性值1;属性名2:属性值3;...")
Example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> </head> <body> <div id="dome">欢迎来到PHP中文网!</div> <script type="text/javascript"> $("#dome").attr("style", "color:pink;"); </script> </body> </html>
Rendering:
Related video tutorial recommendation: jQuery Tutorial(video)
The above is the detailed content of How to set css style for elements in jquery. For more information, please follow other related articles on the PHP Chinese website!