Home > Web Front-end > JS Tutorial > body text

jquery sets class style of element

无忌哥哥
Release: 2018-06-29 13:48:11
Original
1663 people have browsed it

jquery sets the class style of elements

Methods to set element styles

1. Specify the class style rules to be applied by setting the class attribute of the tag

2 .Directly set the style attribute of the tag to reset the style of the current element

1. Apply class style: addClass()

$('#pic').addClass('circle')
$('#pic').addClass('circle shadow')
Copy after login
Copy after login

2. Remove class style: removeClass()

$('#pic').removeClass('circle')
$('#pic').removeClass('shadow')
$('#pic').removeClass('circle shadow')
Copy after login

3. Automatic style switching: toogleClass()

If no class style is added to the current element, then the class style specified by it will be automatically added

$('#pic').toggleClass('circle shadow')
Copy after login
Copy after login

If the current element has been added If the upper class style is used, it will remove this class style

First add the upper class style to the element

$('#pic').addClass('circle')
$('#pic').addClass('circle shadow')
Copy after login
Copy after login

Now the removal operation is performed

$('#pic').toggleClass('circle shadow')
Copy after login
Copy after login

4. Query class style: hasClass()

var res = $('#pic').hasClass('circle shadow') //false
$('#pic').addClass('circle shadow') 
var res = $('#pic').hasClass('circle shadow') //true
if ($('#pic').hasClass('circle shadow')) {
$('#pic').removeClass('circle shadow')
} else {
$('#pic').addClass('circle shadow') 
}
Copy after login

//Console view results

console.log(res)
Copy after login

The above is the detailed content of jquery sets class style of element. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!