jQuery addClass - 可以在同一个 div 上添加多个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11009820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:04:41  来源:igfitidea点击:

addClass - can add multiple classes on same div?

jquery

提问by Hunter

jQuery add multiple class

jQuery 添加多个类

This is my current code, I know its wrong code

这是我当前的代码,我知道它的错误代码

$('.page-address-edit').addClass('test1').addClass('test2');

回答by ShibinRagh

$('.page-address-edit').addClass('test1 test2 test3');

Ref- jQuery

参考 - jQuery

回答by maxdec

You can do

你可以做

$('.page-address-edit').addClass('test1 test2');

More here:

更多在这里

More than one class may be added at a time, separated by a space, to the set of matched elements, like so:

$("p").addClass("myClass yourClass");

一次可以将多个类添加到一组匹配元素中,以空格分隔,如下所示:

$("p").addClass("myClass yourClass");

回答by Al-Mothafar

You can add multiple classes by separating classes names by spaces

您可以通过用空格分隔类名来添加多个类

$('.page-address-edit').addClass('test1 test2 test3');

回答by xdazz

You code is ok only except that you can't add same class test1.

你的代码没问题,只是你不能添加相同的 class test1

$('.page-address-edit').addClass('test1').addClass('test2'); //this will add test1 and test2

And you could also do

你也可以这样做

$('.page-address-edit').addClass('test1 test2');