jQuery直接设置元素样式值
jQuery直接设置元素样式值
在jQuery中,可以通过css()方法为某个指定的元素设置样式值,语法格式:css(name,value)其中name为样式名称,value为样式值。
示例如下:
- <head>
- <title>直接设置元素样式值</title>
- <script type="text/javascript"
- src="../Jscript/jquery-1.4.2.js">
- </script>
- <script type="text/javascript"
- src="../Jscript/jquery-1.4.2-vsdoc.js">
- </script>
- <style type="text/css">
- body{font-size:15px}
- p{padding:5px;width:220px}
- </style>
- <script type="text/javascript">
- $(function() {
- $("p").click(function() {
- $(this).css("font-weight", "bold");//字体加粗
- $(this).css("font-style", "italic");//斜体
- $(this).css("background-color", "#eee");//增加背景色
- })
- })
- </script>
- </head>
- <body>
- <p>Write Less Do More</p>
- </body>
- </html>
评论暂时关闭