jquery如何判断是否存在class


当前第2页 返回上一页

1. 使用is(‘.classname’)的方法

1

$('div').is('.redColor')

2. 使用hasClass(‘classname’)的方法(注意jquery的低版本可能是hasClass(‘.classname’))

1

$('div').hasClass('redColor')

以下是检测一个元素是否含有一个redColor类的例子,含有时,则把其类变为blueColor。

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

27

28

29

30

31

32

33

34

35

36

37

<html>

<head>

<styletype="text/css">

.redColor {

background:red;

}

.blueColor {

background:blue;

}

</style>

<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>

</head>

<body>

<h1>jQuery check if an element has a certain class</h1>

<divclass="redColor">This is a div tag with class name of "redColor"</div>

<p>

<buttonid="isTest">is('.redColor')</button>

<buttonid="hasClassTest">hasClass('.redColor')</button>

<buttonid="reset">reset</button>

</p>

<scripttype="text/javascript">

$("#isTest").click(function () {

if($('div').is('.redColor')){

$('div').addClass('blueColor');

}

});

$("#hasClassTest").click(function () {

if($('div').hasClass('redColor')){

$('div').addClass('blueColor');

}

});

$("#reset").click(function () {

location.reload();

});

</script>

</body>

</html>

相关免费学习推荐:JavaScript(视频)

以上就是jquery如何判断是否存在class的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

jQuery 元素不可用怎么设置

jQuery怎么判断元素是否在数组中

jQuery如何设置行不同的颜色

jQuery如何验证元素是否为空

jQuery ajax 提交乱码怎么办

jQuery 如何通过文本查找元素

jQuery怎么改变this指向

jQuery 不支持on怎么办

jQuery mobile怎么样

jQuery如何解除事件绑定?

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




打赏

取消

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

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

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

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

评论

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