search
How to use jqueryDec 05, 2018 pm 04:28 PM
jquery

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.

How to use jquery

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?

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 to

download 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:

a. Loading jQuery from the Microsoft AJAX CDN

Can be loaded from Microsoft AJAX CDN loads jQuery files. More details are available here. You need to keep the following markup in your page.

<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 API

You 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 faster

2. 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=&#39;text/javascript&#39;>//<![CDATA[
        if (typeof jQuery == &#39;undefined&#39;) {
            document.write(unescape("%3Cscript 
    src=&#39;/Script/jquery-1.4.1.min.js&#39; type=&#39;text/javascript&#39; %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!

Statement
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
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

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

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

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

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

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

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

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

jquery中apply()方法怎么用jquery中apply()方法怎么用Apr 24, 2022 pm 05:35 PM

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

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

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

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

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

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

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

Atom editor mac version download

The most popular open source editor