Home > Article > Web Front-end > How to use jQuery inner class name selector
This time I will show you how to use jQuery's internal class name selector, and what are the precautions when using jQuery's internal class name selector. The following is a practical case, let's take a look.
1. Introduction
The class name selector finds matching DOM elements by the name of the CSS class owned by the element.
In a page, an element can have multiple CSS classes, and a CSS class can match multiple elements. If there is a matching class name in an element, it can be selected by the class name selector. .
The class name selector is easy to understand. Most people must have chosen courses when they were in college. The CSS class name can be understood as the course name, and the elements can be understood as students. Students can choose multiple courses, and A course can be chosen by multiple students.
The relationship between CSS classes and elements can be a many-to-many relationship, a one-to-many or many-to-one relationship. Simply put, the class name selector is to find matching elements based on the CSS class name of the element.
The class name selector is used as follows:
$(".class");
where class is the CSS class name used to query the element.
For example, to query for elements using the CSS class name word_orange, you can use the following jQuery code:
$("word_orange");
2. Application
In the page, first add two
tags, and set the CSS class for one of them, then select the
tag with the CSS class set through jQuery's class name selector, and set its CSS styles.
3. Code
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <p class="myClass">注意观察我的样式</p> <p>我的样式是默认的</p> <script type="text/javascript"> $(document).ready( function() { var myClass = $(".myClass"); //选取DOM元素 myClass.css("background-color","#C50210"); //为选取的DOM元素设置背景颜色 myClass.css("color","#FFF"); //为选取的DOM元素设置文字颜色 }); </script>
4. Running effect
##5. Operation instructions
In the above code, CSS is only set for one of thetags Class name, but since there is no CSS class named myClass in the program, this class does not have any attributes.
The class name selector will return a jQuery packaging set named myClass. Thecss() method can be used to set the CSS attribute value for the corresponding p element. Here, the background color of the element is Set to dark red and text color to white.
Detailed explanation of the use of common built-in functions in JS
How to use js to encapsulate ajax function functions and usage
The above is the detailed content of How to use jQuery inner class name selector. For more information, please follow other related articles on the PHP Chinese website!