How to use jquery: First load the jquery file through CDN loading or local loading of files; then execute the jQuery code through the jquery file.
The operating environment of this article: Windows 7 system, Dell G3 computer, jquery version 3.2.1.
How to use jquery? This article will introduce to you what is jquery? Why use jquery? How to use jquery? Let everyone know how to use jquery, I hope it will be helpful to you.
What is jQuery?
jQuery is not a language, but it is well-written JavaScript code. It is a fast and concise JavaScript library that simplifies HTML document traversal, event handling, animation and Ajax Interactive for rapid web development.
Why choose jQuery?
jQuery is a very compact and well-written JavaScript code that improves developer productivity by writing very small amounts of code so that they can implement critical UI functionality.
The advantages of jQuery are:
1. There is no need to learn a new syntax to use jQuery, it is enough to understand simple JavaScript syntax.
2. The code is simple and clear, and complex functions can be implemented without writing multiple lines of code.
3. Helps improve application performance.
4. It helps develop web pages that are compatible with most browsers.
5. It helps to implement key UI-related functions without writing hundreds of lines of code.
How to use jQuery?
#1. Load the jQuery file
jQuery usually comes as a single JavaScript file that contains everything jQuery does out of the box. It can be included in a web page in the following ways:1), load local jQuery file
First you need todownload the jQuery file to the local, jQuery JavaScript files can be downloaded from the jQuery official website, address: http://www.jquery.com.
Then load jQuery files<script type="text/javascript" src="jQuery-1.4.1-min.js"></script>
2), load jQuery from CDN
What is CDN?
CDN stands for Content Distribution Network, also known as Content Delivery Network. It is a group of computers placed at various points connected to the replica network containing data files to maximize bandwidth. In a CDN, clients access a copy of the data closer to the client's location, rather than all clients accessing it from one specific server. This helps achieve better data retrieval performance on the client side.There are two main CDNs available for hosting jQuery files:
<script type="text/javascript" language="Javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.min.js"></script>b. Loading jQuery from Google Libraries APIYou can load jQuery files from Google CDN. You need to keep the following markup in your page.
<script type="text/javascript" language="Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"> </script>
Benefits of loading jQuery from CDN:
1. Since there is no need to download the jQuery file, the page loads faster2. Load jQuery files from the server, thus saving bandwidth 3. Scalable, usually CDNs place these files on servers located in different geographical locations around the world so that they load faster, so no matter where the user is browsing page, and your application will run normally.Note:
Generally, loading jQuery from CDN and loading jQuery files from local area must be written in order to prevent loading jQuery files. CDN is not available. Therefore, you can write the following lines of code:<!-- START - jQuery Reference --> <script type="text/javascript" language="Javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.min.js"></script> <script type='text/javascript'>//<![CDATA[ if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/Script/jquery-1.4.1.min.js' type='text/javascript' %3E%3C/script%3E")); }//]]> </script> <!-- END - jQuery Reference -->
2. Execute jQuery code
We may have two ways to execute jQuery code. 1. When the page loads, execute the jQuery code:<script language="javascript" type="text/javascript"> $(function () { $("#div1").css("border", "2px solid green"); }); </script>or:
<script language="javascript" type="text/javascript"> $("#div1").css("border", "2px solid green"); </script>The advantage of executing jQuery code in this way is that it will not wait The entire page loads completely, so you can use this if you want the user to see the effect as soon as the corresponding element loads. But the disadvantage is that if the element that jQuery has to execute is not loaded, then it will error or you will not get the desired result; therefore, when using this way of executing jQuery code, you must make sure first Load the element where you want to use jQuery (you can put the jQuery code after the HTML element). 2. Only execute jQuery when the complete DOM object (complete page has been loaded). At this point the code must be wrapped in a .ready function.
<script language="javascript" type="text/javascript"> $(document).ready(function () { $("#div1").css("border", "2px solid green"); }); </script>
这是执行jQuery的更好,更安全的方法。这样可以确保只有在浏览器中加载完整页面时才会执行jQuery代码,因此可以放心,用户不会在页面上看到任何不需要的行为。
总结:以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。
The above is the detailed content of How to use jquery. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
