The first lecture on jquery basics: understanding jquery_jquery
jQuery is an excellent JavaScript library. With its concise syntax and cross-platform compatibility, it greatly simplifies developers to traverse HTML documents, operate DOM, handle events, perform animations and develop Ajax operations.
jQuery Advantages:
1.Lightweight
2. Powerful selector
3. Excellent encapsulation of DOM operations
4. Reliable event handling mechanism
5. Perfect Ajax
6. Do not pollute top-level variables
7. Excellent browser compatibility
8. Chain operation mode
9. Implicit iteration
10. Separation of behavioral layer and structural layer
11. Rich plug-in support
12. Complete documentation
13. Open source
(1) Write simple jQuery code:
Note: $ is the abbreviation of jQuery.
<!-- 引入 jQuery --> <script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { //等待DOM元素加载完毕.(可能DOM元素关联的文件没有加载完) alert("Hello World!"); //弹出一个框 }); $(document).ready(function () { //可以同时编写多个 alert("Hello again!"); }); $(function () { //简写形式 alert("Hello jQuery!"); }); </script>
(2) jQuery code style
Note: Chain operation.
1. No more than 3 operations on the same object can be written in one line.
2. If there are many operations on the same object, it is recommended to write one operation per line.
3. For a small number of operations on multiple objects, you can write one line for each object. If sub-elements are involved, appropriate indentation can be considered.
4. For more operations on multiple objects, 2 or 3 can be combined.
Add necessary comments.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>1-4-3</title> <style type="text/css"> #menu { width:300px; } .has_children{ background : #555; color :#fff; cursor:pointer; } .highlight{ color : #fff; background : green; } div{ padding:0; } div a{ background : #888; display : none; float:left; width:300px; } </style> <!-- 引入 jQuery --> <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script> <script type="text/javascript"> //等待dom元素加载完毕. $(document).ready(function(){ $(".has_children").click(function(){ $(this).addClass("highlight") //为当前元素增加highlight类 .children("a").show().end() //将子节点的a元素显示出来并重新定位到上次操作的元素 .siblings().removeClass("highlight") //获取元素的兄弟元素,并去掉他们的highlight类 .children("a").hide(); //将兄弟元素下的a元素隐藏 }); }); </script> </head> <body> <div id="menu"> <div class="has_children"> <span>第1章-认识jQuery</span> <a>1.1-JavaScript和JavaScript库</a> <a>1.2-加入jQuery</a> <a>1.3-编写简单jQuery代码</a> <a>1.4-jQuery对象和DOM对象</a> <a>1.5-解决jQuery和其它库的冲突</a> <a>1.6-jQuery开发工具和插件</a> <a>1.7-小结</a> </div> <div class="has_children"> <span>第2章-jQuery选择器</span> <a>2.1-jQuery选择器是什么</a> <a>2.2-jQuery选择器的优势</a> <a>2.3-jQuery选择器</a> <a>2.4-应用jQuery改写示例</a> <a>2.5-选择器中的一些注意事项</a> <a>2.6-案例研究——类似淘宝网品牌列表的效果</a> <a>2.7-还有其它选择器么?</a> <a>2.8-小结</a> </div> <div class="has_children"> <span>第3章-jQuery中的DOM操作</span> <a>3.1-DOM操作的分类</a> <a>3.2-jQuery中的DOM操作</a> <a>3.3-案例研究——某网站超链接和图片提示效果</a> <a>3.4-小结</a> </div> </div> </body> </html>
(3) DOM object and jQuery object
DOM (Document Object Model, Document Object Model), each DOM can be represented as a tree. GetElementById or getElementByTagName in JavaScript is used to obtain element nodes. Such DOM elements are DOM objects.
jQuery objects are objects generated by wrapping DOM objects through jQuery. The jQuery object is unique to jQuery. You can use the methods in jQuery, but you cannot use the methods of the DOM object.
Note: jQuery objects are all manufactured through the $() function, which is a jQuery object manufacturing factory.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>1-4</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- 引入 jQuery --> <script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script> <script type="text/javascript"> //等待dom元素加载完毕. $(document).ready(function () { var domObj = document.getElementsByTagName("h3")[0]; // Dom对象 var $jQueryObj = $(domObj); //jQuery对象 alert("DOM对象:" + domObj.innerHTML); alert("jQuery对象:" + $jQueryObj.html()); }); </script> </head> <body> <h3>例子</h3> <p title="选择你最喜欢的水果.">你最喜欢的水果是?</p> <ul> <li>苹果</li> <li>橘子</li> <li>菠萝</li> </ul> </body> </html>
DOM objects and jQuery objects can be converted to each other. The examples are as follows:
1.DOM object method
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>1-6-2</title> <!-- 引入 jQuery --> <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script> <script type="text/javascript"> //等待dom元素加载完毕. $(document).ready(function () { var $cr = $("#cr"); //jQuery对象 var cr = $cr.get(0); //DOM对象获取 2种方式 $cr[0] 或者 $cr.get(0) $cr.click(function () { if (cr.checked) { //DOM方式判断 alert("感谢你的支持!你可以继续操作!"); } }) }); </script> </head> <body> <input type="checkbox" id="cr" /> <label for="cr">我已经阅读了上面制度.</label> </body> </html>
2.jQuery object method
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>1-6-1</title> <!-- 引入 jQuery --> <script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script> <script type="text/javascript"> //等待dom元素加载完毕. $(document).ready(function () { var cr = document.getElementById("cr"); //DOM对象 var $cr = $(cr); //jQuery对象 $cr.click(function () { if ($cr.is(":checked")) { //jQuery方式判断 alert("感谢你的支持!你可以继续操作!"); } }) }); </script> </head> <body> <input type="checkbox" id="cr" /> <label for="cr">我已经阅读了上面制度.</label> </body> </html>
The above is the first lesson about jquery learning, I hope you will continue to pay attention.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
