Home >Web Front-end >JS Tutorial >How to change the background color of article paragraphs through the jquery toggleClass() attribute

How to change the background color of article paragraphs through the jquery toggleClass() attribute

不言
不言Original
2018-07-04 11:48:001949browse

This article mainly introduces the method of changing the background color of article paragraphs through the jquery toggleClass() attribute. It has a certain reference value. Now I share it with you. Friends in need can refer to it

jQuery Make an article paragraph to change the background color attribute jquery toggleClass() attribute. The article attaches example code and source code for everyone. Interested friends can refer to it

Definition and usage

toggleClass() method adds and Removes one or more classes from selected elements to toggle.

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)
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 rendering is as follows:

File reference:

<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>

Above That’s the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

jquery introduces the method of introducing external CDN and importing the local jq library if loading fails

jquery.onoff implementation Switch button function

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

Statement:
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