jQuery basic selector

BeginnerjQuery, in order to learn jQuery better systematically, today I specially summarize my learning experience and post it to share with the beginners. Today I mainly summarize the basic selectors of jQuery. The basic selector of

jQuery is the most commonly used selector in jQuery and is also the simplest selector. It mainly uses the ID# of the element. ##, CLASS, element tag ELEMENT, etc. to find the DOM element in HTML. In a web page, each ID name can only be used once, and class can be used repeatedly. In jQury applications, you can use these basic selectors to complete most of the work. Let's mainly take a look at their specific implementation process. In order to learn better, we first list an HTML tag code here:

<div id="mydiv">
    <div class="mini">我是一个名为mini的div标签</div>
    <div class="mini">我是一个名为mini的div标签</div>
    <div class="mini">我是一个名为mini的div标签</div>
    <p>我是一个段落p标签</p>
    <span>我是一个行内span标签</span>
<div>

Then let’s look at the applications of these basic selectors respectively

1. ID Selector

Selector: #id

Description: Match an element based on the given id

Return: Single element

Example:

<script type="text/javascript">    $(document).ready(function(){
        //id选择器        $("#mydiv").css("background","#f96");
     });</script>

Function:Change the id to mydiv The background color of the element

2. Class selector

Selector:.class

Description:Return elements matching the given class name

Return: Collection elements

Example:

<script type="text/javascript">    $(document).ready(function(){
        //class选择器        $(".mini").css("background","#f96");
     });</script>

Function: Change the background color of all elements with class mini

3. Tag element

Selector: element

Description: Match elements based on the given element name

Return: Collection elements

Example:

<script type="text/javascript">    $(document).ready(function(){
        //element选择器        $("div").css("background","#f96");
     });</script>

Function:Change the background color of all elements whose element name is <div>

4. All elements*

Selector: *

Description: Change all matching html tag elements

Return value: Collection elements

Example:

<script type="text/javascript">    $(document).ready(function(){
        //*选择器        $("*").css("background","#f96");
     });</script>

Function:Change the background color of all html element tags

5. selector1, selector2,....selectorN

##Selector:

selector,selector2,...selectorN

Description:

will each The elements matched by the selector are combined and returned together

Return:

Collection elements

Example:

<script type="text/javascript">    $(document).ready(function(){
        //selector1,selector2,...selectorN选择器        $(".mini,p").css("background","#f96");
     });</script>

Function:Change the background color of the class name mini and paragraph p elements

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title></title> <style> div { width: 100px; height: 90px; float: left; padding: 5px; margin: 5px; background-color: #EEEEEE; } </style> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <body> <div id="aaron"> <p>id="aaron"</p> <p>选中</p> </div> <div id="imooc"> <p>id="php中文网"</p> <p>jQuery选中</p> </div> <div id="imooc"> <p>id="php中文网"</p> <p>jQuery未选中</p> </div> <script type="text/javascript"> //通过原生方法处理 var div = document.getElementById('aaron'); div.style.border = "3px solid blue"; </script> <script type="text/javascript"> //通过jQuery直接传入id //id的唯一,只选择到了第一个匹配的id为php中文网的div节点 $("#imooc").css("border", "3px solid red"); </script> </body> </html>
submitReset Code