Home > Web Front-end > JS Tutorial > jquery learning two attributes (class)_jquery

jquery learning two attributes (class)_jquery

WBOY
Release: 2016-05-16 18:15:33
Original
1173 people have browsed it

addClass(class)

Adds the specified class name to each matched element.

Return value

jQuery

Parameters

class (String): One or more CSS class names to be added to the element, please separate them with spaces

Example

Add the 'selected' class to the matched elements

HTML code:

Hello

jQuery code:

$("p").addClass("selected");

Result:

[

Hello

]

Add selected highlight class to matched elements

HTML code:

Hello

jQuery code:

$("p").addClass("selected highlight");

Result:

[

Hello

]
-------------------------------------------------- -------------------------------------------------- --------------------------

removeClass(class)

Remove all or specified classes from all matching elements.

Return value

jQuery

Parameters

class (String): (optional) One or more CSS class names to be deleted, please separate them with spaces

Example

Remove 'selected' class from matched elements

HTML code:

Hello

jQuery code:

$("p").removeClass("selected");

Result:

[

Hello

]

Remove all classes of matching elements

HTML code:

Hello

jQuery code:

$("p").removeClass();

Result:

[

Hello

]
-------------------------------------------------- -------------------------------------------------- --------------------------

toggleClass(class)

Delete (add) a class if it exists (does not exist).

Return value

jQuery

Parameters

class (String): CSS class name

Example

Toggle 'selected' class for matching elements

HTML code:

Hello

Hello Again

jQuery code:

$("p").toggleClass("selected");

Result:

[

Hello

,

Hello Again

]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template