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

How to use jquery toggleClass() attribute to create article paragraphs and change the background color

php中世界最好的语言
Release: 2018-05-28 14:22:06
Original
1871 people have browsed it

This time I will show you how to use jquery toggleClass()AttributesCreate article paragraphs to change the background color, use jquery toggleClass() attribute to create article paragraphs to change the background colorNotes What are they? Here are actual cases. Let’s take a look.

Definition and Usage

The toggleClass() method switches one or more classes for adding and removing selected elements.

This method checks the specified class in each element. Adds the class if it does not exist, or removes it if it is set. This is called a toggle effect.

However, by using the "switch" parameter, you can specify that only classes be removed or only added.

Syntax

$(selector).toggleClass(classname,function(index,currentclass),switch)
Copy after login
Parameters Description
classname Required. Specifies one or more class names to be added or removed. If you need to specify several classes, use spaces to separate class names.
function(index,currentclass) Optional. Specifies a function that returns one or more class names that need to be added/removed.
  • index - Returns the index position of the element in the collection.

  • currentclass - Returns the current class name of the selected element.

switch Optional. Boolean value that specifies whether to only add (true) or remove (false) classes.

Introduction to usage:

jQuery creates article paragraphs to change the background color attribute jquery toggleClass() attribute.

The effect diagram is as follows:

Effect display Source code download

FileQuote

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $("input").click(function(){
  $("p").toggleClass("main");
 });
});
</script>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to globally configure the 404 page for Vue applications

How to use nodejs express to configure a self-signed https server

The above is the detailed content of How to use jquery toggleClass() attribute to create article paragraphs and change the background color. For more information, please follow other related articles on the PHP Chinese website!

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