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>
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:
<!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>
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>
(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>
(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>
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!