简体   繁体   English

如何使用 javascript 将多个 css 类添加到 html 元素

[英]how to add multiple css classes to a html element usin javascript

I want to add multiple classes to an html element我想向 html 元素添加多个类

I know the following code is possible.我知道以下代码是可能的。

$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry")

But i want to know if the following code is also possible.?但我想知道下面的代码是否也是可能的。?

$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry1")
                    .addClass("entry2")
$("ul#List").find("li.template")
            .removeClass("template")
            .addClass("entry1 entry2")

Edit: Take a look http://jsfiddle.net/nHs9W/编辑:看看http://jsfiddle.net/nHs9W/

Both of the below snippets work以下两个片段都有效

.addClass("entry1 entry2");

.addClass("entry1").addClass("entry2");

you can add multiple classes separated by a space:您可以添加多个由空格分隔的类:

.addClass("entry1 entry2")

-1 for not reading the manual ! -1 不阅读手册

But anyway, from that page,但无论如何,从那个页面,

.addClass( className ) .addClass(类名)
className: One or more class names to be added to the class attribute of each matched element. className:要添加到每个匹配元素的 class 属性中的一个或多个 class 名称。

The most important part being one or more最重要的部分是一个或多个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM