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

How to use jQuery? What are jQuery selectors?

青灯夜游
Release: 2018-11-13 10:39:38
forward
2196 people have browsed it

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.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
</head>
<body>
</body>
</html>
Copy after login

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()
Copy after login

Example:

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

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(){     
       // 代码部分写在这里     
    });
Copy after login

can also be abbreviated as follows:

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

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
           $("button").click(function(){
                $("#myDiv1").html("Hello JQuery World");
                $("#myDiv1").css("background-color","green");
            });
        });
    </script>
</head>
<body>
    <button type="button">点击</button>
    <div id="myDiv1">Hello</div>
</body>
</html>
Copy after login

 

3. Basic selector

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

Example: Use the element selector to select all

elements and hide them.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
           $("button").click(function(){
                $("p").hide();
            });
         
        });
    </script>
</head>
<body>
    <button type="button">点击</button>
    <p>p元素1</p>
    <p>p元素2</p>
    <div id="myDiv1">Hello</div>
</body>
</html>
Copy after login

 

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
           $("button").click(function(){
                $("#myDiv1").hide();
            });
        });
    </script>
</head>
<body>
    <button type="button">点击</button>
    <p>p元素1</p>
    <p>p元素2</p>
    <div id="myDiv1">Hello</div>
</body>
</html>
Copy after login

 

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
           $("button").click(function(){
                $(".myClass1").hide();
            });
        });
    </script>
</head>
<body>
    <button type="button">点击</button>
    <p>p元素1</p>
    <p>p元素2</p>
    <div id="myDiv1">Hello</div>
    <div Class="myClass1">你好</div>
</body>
</html>
Copy after login

 

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!

Related labels:
source:cnblogs.com
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