如何在JQuery中为Div元素添加css类?

时间:2022-09-28 20:23:42

How do I add a css class to an Div element in JQuery? I have a DIV with a css class and the div contains a form. After clicking on submit I want the original css class to disappear and a new class to be added? I'm using JQuery as a Framework

如何在JQuery中为Div元素添加css类?我有一个带有css类的DIV,div包含一个表单。单击提交后,我希望原始的css类消失,并添加一个新类?我正在使用JQuery作为框架

4 个解决方案

#1


$("#element_id").removeClass( "oldclass").addClass("newclass");

#2


To add a class, use addClass:

要添加类,请使用addClass:

$('#myDiv').addClass("blah");

So upon your submit you can do something like:

因此,在您提交后,您可以执行以下操作:

$('#submit').click(function() {
    $('#myDiv').removeClass().addClass("blah");
});

Calling removeClass with no parameters removes all classes linked to that element. Alternatively, you can call removeAttr("style")

不带参数调用removeClass会删除链接到该元素的所有类。或者,您可以调用removeAttr(“style”)

#3


I think you can use

我想你可以用

$("#target").addClass("newclass");

for adding the class to the elemet #target and

用于将类添加到elemet #target和

$("#target").removeClass("newclass");

to remove it.

删除它。

If you dont know wether the class is already there or not you can use

如果你不知道课程是否已经存在,你可以使用

$("#target").toggleClass("newclass");

if its there, it will be removed, and if its not there it will be added.

如果它在那里,它将被删除,如果不存在它将被添加。

#4


Since OP didn't mention any ids I would go with a class selector and picking the first element in collection (which could be wrong, offcourse).

由于OP没有提到任何id,我会选择一个类选择器并选择集合中的第一个元素(这可能是错误的,offcourse)。

$(".OriginalClass").removeClass("OriginalClass").addClass("blah");

The original example was wrong, as the elements in the collection are regular objects and as such they don't have the removeClass and addClass methods.

原始示例是错误的,因为集合中的元素是常规对象,因此它们没有removeClass和addClass方法。

The current example removes the class for all elements, which is still flawed, but in my opinion works better with what OP wanted.

当前示例删除了所有元素的类,这仍然存在缺陷,但在我看来,OP的效果更好。

#1


$("#element_id").removeClass( "oldclass").addClass("newclass");

#2


To add a class, use addClass:

要添加类,请使用addClass:

$('#myDiv').addClass("blah");

So upon your submit you can do something like:

因此,在您提交后,您可以执行以下操作:

$('#submit').click(function() {
    $('#myDiv').removeClass().addClass("blah");
});

Calling removeClass with no parameters removes all classes linked to that element. Alternatively, you can call removeAttr("style")

不带参数调用removeClass会删除链接到该元素的所有类。或者,您可以调用removeAttr(“style”)

#3


I think you can use

我想你可以用

$("#target").addClass("newclass");

for adding the class to the elemet #target and

用于将类添加到elemet #target和

$("#target").removeClass("newclass");

to remove it.

删除它。

If you dont know wether the class is already there or not you can use

如果你不知道课程是否已经存在,你可以使用

$("#target").toggleClass("newclass");

if its there, it will be removed, and if its not there it will be added.

如果它在那里,它将被删除,如果不存在它将被添加。

#4


Since OP didn't mention any ids I would go with a class selector and picking the first element in collection (which could be wrong, offcourse).

由于OP没有提到任何id,我会选择一个类选择器并选择集合中的第一个元素(这可能是错误的,offcourse)。

$(".OriginalClass").removeClass("OriginalClass").addClass("blah");

The original example was wrong, as the elements in the collection are regular objects and as such they don't have the removeClass and addClass methods.

原始示例是错误的,因为集合中的元素是常规对象,因此它们没有removeClass和addClass方法。

The current example removes the class for all elements, which is still flawed, but in my opinion works better with what OP wanted.

当前示例删除了所有元素的类,这仍然存在缺陷,但在我看来,OP的效果更好。