jquery-Basic Understanding

Source: Internet
Author: User

* * * * What is jquery:

JQuery is A library of JS, encapsulated in our development process commonly used in some of the features, convenient for us to call, improve our development efficiency.

Why use jquery:

1. Native JS provides too few methods to manipulate the DOM. and compatibility is not good.

2, the function of jquery is powerful.

    • A. Lightweight
    • B. Excellent browser compatibility
    • C. Excellent DOM operation
    • D. Chained operation mode
    • E. Implicit iteration set (you can automatically iterate over the collection of selected objects without needing a in loop) such as $ ("Div:lt (2)"). CSS ("Border-color", "red")/* adds css*/to the first two Div
    • F. Separation of the behavior layer from the structure (if you want to add an event to the DOM, add the function to be declared once in JavaScript, and use it once in the DOM, jquery simply declares an event of an element in the jquery section: $ ("#myButton"). Click (function () {code part}))
    • G. Support for extensions
    • H. Complete Learning Resources
    • I. Open source
How to use jquery:

A . Primer Package

Before using jquery , first bring the jquery file to the page. If you do not reference a jquery file before using jquery , or if the src path is incorrectly written, you will get an error.

two . Entry function

Writing one:

$ (document). Ready (function() {        $ ("#btn"). Click (function() {            Alert ("event is bound");        })    })

Two:

$ (function() {        $ ("#btn"). Click (function() {            alert ("event is bound");        })    }) 

The effect is exactly the same, so how to write the entry function in development? You know ....

Here is the "$" in jquery, which actually represents a function that is an object of the jquery framework exposed to the global.

In addition to $ , there is another function:jquery, that is, where the $ symbol appears, you can replace it with jquery, and write $ for shorthand.

Here, think of the native JS entry function window.onload = Functions () {};, so what is the difference between the entry functions?

Difference One: The number of writing is different

Js Entry function can only occur once, there will be multiple event coverage issues.

the entry function for JQuery can occur any number of times, and there is no event coverage problem.

Difference two: The timing of execution is different

The Js entry function is executed only after all the file resources have been loaded. These file resources include: page document, external js file, external CSS file, image, etc.

the entry function of JQuery is executed after the document has been loaded. The completion of the document loading here meansthat after theDom tree is loaded, the DOM can be manipulated without waiting for all external resources to be loaded.

3. function implementation code (event handling)

This part has to say about the mutual conversion of jquery objects (using jquery to manipulate DOM return values) and Dom objects (using JS to manipulate the return of the DOM).

var // BTN is a DOM object

var // $btn is a jquery object

var // This translates the DOM object btn into a jquery object $btn1

The JQuery object is converted into a DOM object:

Method One:

var // The jquery object $btn is converted to DOM object Btn1 (this is the recommended way)

Method Two:

var btn2 = $btn. Get (0); // at this point, the JQuery object $btn is converted into a DOM object btn2

In short: jquery gets the DOM object and encapsulates it with the jquery object of the jquery method , plainly is to repack the DOM object.

This section is about this, and the next chapter is about the main functional modules of jquery.

jquery-Basic Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.