Home  >  Article  >  Web Front-end  >  How to use jQuery? What are jQuery selectors?

How to use jQuery? What are jQuery selectors?

青灯夜游
青灯夜游forward
2018-11-13 10:39:032138browse

The content of this article is to introduce how to use jQuery? What are jQuery selectors? Let everyone understand the use of the jQuery file library, the use of basic syntax, and the jQuery selector. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. How to use

The jQuery library is located in a JavaScript file, which contains all jQuery functions.

When a web page needs to use jQuery, you need to introduce jQuery's js file into the web page first.

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
6cee78a5815e1ea034fd9f5df4179a0c
b2386ffb911b14667cb8f0f91ea547a7My Test JQuery6e916e0f7d1e588d4f442bf645aedb2f
    9b264bb87e5a392a4202120e2b17019f2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

2. jQuery syntax

jQuery syntax is to select HTML elements and perform certain operations on the selected elements. [Recommended related video tutorials: jQuery Tutorial]

Basic grammar form:

 $(selector).action()

Example:

$("p").hide()    // 隐藏所有 <p> 元素
$("#myInfo").hide() // 隐藏所有 id="myInfo" 的元素

Writing:

All jQuery functions are located in a document.ready function. As shown below.

This is to prevent jQuery code from running before the document is fully loaded (ready), that is, the DOM cannot be manipulated until the DOM is loaded.

If you run the function before the document is fully loaded, the operation may fail.

    $(document).ready(function(){     
       // 代码部分写在这里     
    });

can also be abbreviated as follows:

   $(function(){         
      // 这里写代码         
   });

Example:

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
6cee78a5815e1ea034fd9f5df4179a0c
b2386ffb911b14667cb8f0f91ea547a7My Test JQuery6e916e0f7d1e588d4f442bf645aedb2f
    9b264bb87e5a392a4202120e2b17019f2cacc6d41bbb37262a98f745aa00fbf0
    ea1aab715d1b735c8b112b6fff119ca8    
        $(function(){
           $("button").click(function(){
                $("#myDiv1").html("Hello JQuery World");
                $("#myDiv1").css("background-color","green");
            });
        });
    2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
    285f2ee1e472a25f04d29214b2020631点击65281c5ac262bf6d81768915a4a77ac0
    78a54985854ba9d1968e744276530244Hello16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

 

3. Basic selector

(1) Element Selector: The jQuery element selector selects elements based on their names.

Example: Use the element selector to select all e388a4556c0f65e1904146cc1a846bee elements and hide them.

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
6cee78a5815e1ea034fd9f5df4179a0c
b2386ffb911b14667cb8f0f91ea547a7My Test JQuery6e916e0f7d1e588d4f442bf645aedb2f
    9b264bb87e5a392a4202120e2b17019f2cacc6d41bbb37262a98f745aa00fbf0
    ea1aab715d1b735c8b112b6fff119ca8    
        $(function(){
           $("button").click(function(){
                $("p").hide();
            });
         
        });
    2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
    285f2ee1e472a25f04d29214b2020631点击65281c5ac262bf6d81768915a4a77ac0
    e388a4556c0f65e1904146cc1a846beep元素194b3e26ee717c64999d7867364b1b4a3
    e388a4556c0f65e1904146cc1a846beep元素294b3e26ee717c64999d7867364b1b4a3
    78a54985854ba9d1968e744276530244Hello16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

 

(2) #id selector: jQuery #id selector selects the specified element through the id attribute of the HTML element.

The id of the element in the page should be unique, so if you want to select the only element in the page, you need to use the #id selector

Example: Use the #id selector to select id="myDiv1 " element to hide it.

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
6cee78a5815e1ea034fd9f5df4179a0c
b2386ffb911b14667cb8f0f91ea547a7My Test JQuery6e916e0f7d1e588d4f442bf645aedb2f
    9b264bb87e5a392a4202120e2b17019f2cacc6d41bbb37262a98f745aa00fbf0
    ea1aab715d1b735c8b112b6fff119ca8    
        $(function(){
           $("button").click(function(){
                $("#myDiv1").hide();
            });
        });
    2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
    285f2ee1e472a25f04d29214b2020631点击65281c5ac262bf6d81768915a4a77ac0
    e388a4556c0f65e1904146cc1a846beep元素194b3e26ee717c64999d7867364b1b4a3
    e388a4556c0f65e1904146cc1a846beep元素294b3e26ee717c64999d7867364b1b4a3
    78a54985854ba9d1968e744276530244Hello16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

 

(3) .class selector: The jQuery class selector can find elements through the specified class.

Example: Use the class selector to select the element with Class="myClass1" and hide it.

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
6cee78a5815e1ea034fd9f5df4179a0c
b2386ffb911b14667cb8f0f91ea547a7My Test JQuery6e916e0f7d1e588d4f442bf645aedb2f
    9b264bb87e5a392a4202120e2b17019f2cacc6d41bbb37262a98f745aa00fbf0
    ea1aab715d1b735c8b112b6fff119ca8    
        $(function(){
           $("button").click(function(){
                $(".myClass1").hide();
            });
        });
    2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
    285f2ee1e472a25f04d29214b2020631点击65281c5ac262bf6d81768915a4a77ac0
    e388a4556c0f65e1904146cc1a846beep元素194b3e26ee717c64999d7867364b1b4a3
    e388a4556c0f65e1904146cc1a846beep元素294b3e26ee717c64999d7867364b1b4a3
    78a54985854ba9d1968e744276530244Hello16b28748ea4df4d9c2150843fecfba68
    690022410a1995d4625bdc4d50bc75bc你好16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

 

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of How to use jQuery? What are jQuery selectors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete