jquery怎么添加css样式


本文摘自PHP中文网,作者藏色散人,侵删。

jquery添加css样式的方法:1、使用addClass()方法添加css样式,语法如“$(selector).addClass(class)”;2、使用css()方法设置css样式,语法如“css("propertyname")”。

本文操作环境:windows7系统、jquery1.2.6版、Dell G3电脑。

在运用jquery的时候我们经常需要动态的给一些dom元素添加CSS样式,下面我们来看一下Jquery如何动态的添加css样式。

jquery添加css样式的方法:

使用addClass() 方法添加css样式。

使用css() 方法设置css样式。

1、使用addClass() 方法添加css样式

addClass() 方法向被选元素添加一个或多个类。该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。

注:如需添加多个类,请使用空格分隔类名。

语法:

1

$(selector).addClass(class)

示例:

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

<html>

<head>

<script type="text/javascript" src="/jquery/jquery.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>

【推荐学习:jquery视频教程】

阅读剩余部分

相关阅读 >>

jQuery如何给p标签赋值

jQuery中$("#")与$("#"+xx)的差异

jQuery怎么去除字符串的空格?

jQuery如何将a标签隐藏了

jQuery如何判断输入框是否为空

jQuery focus()怎么用

详解jQuery中extend()和jQuery.fn.extend()的区别

怎么用jQuery做图片轮播

基于jQuery和html5的日历时钟插件 的图文详解

jQuery是干什么的

更多相关阅读请进入《jQuery》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...