Basic Introduction to jquery Common Operations _ jquery

Source: Internet
Author: User
JQuery is another excellent Javascr into pt framework after prototype. Its purpose is to -- WRITELESS, DOMORE, write less code and do more things. JQuery was created by John Resig, an American who has attracted many javascript experts from around the world to join the team, including Jörn Zaefferer from Germany and Stefan Petre from Romania.
JQuery is another excellent Javascr into pt framework after prototype. Its purpose is to write less, do more, write less code, and do more.
It is a lightweight js Library (only 21 k after compression), which is not suitable for other js libraries. It is compatible with CSS3 and various browsers (IE 6.0 +, FF 1.5 +, safari 2.0 +, Opera 9.0 + ).
JQuery is a fast and concise javaScript library that allows you to easily process HTML documents, events, animation effects, and provide AJAX interaction for websites.
Another major advantage of jQuery is its comprehensive documentation and detailed description of various applications. There are also many mature plug-ins to choose from.
JQuery can ensure that the user's html page is separated from the code and html content. That is to say, you don't need to insert a bunch of JavaScript code in the html to call the command. You just need to define the id.
Jquery is another excellent Javascr into pt framework after prototype. I don't know much about prototype. But after using jquery, she immediately attracted her elegance. Someone uses this analogy to compare prototype and jquery: prototype is like Java, while jquery is like ruby. in fact, I prefer java (less familiar with Ruby), but jquery's simple utility is indeed quite attractive! In the project, I use jquery as my only framework class package. I have a little experience in using it. In fact, these experiences may also be mentioned in the jquery document, but I 'd like to remember them.
I. found you!
Remember $ (), right? Both prototype and DWR use this function instead of document. getElementById (). That's right. jquery also follows suit. To achieve the purpose of document. getElementById (), jquery writes the following:
Code

The Code is as follows:


Var someElement = $ ("# myId ");


It seems to be one more # than the other two frameworks. Let's take a look at the following usage:
Code

The Code is as follows:


$ ("P"); // (1)
$ ("P. container"); // (2)
$ ("P # msg"); // (3)
$ ("Table a", context); // (4)


Have you read this method in prototype? The first line of code gets the P element under all tags. The second line of code gets the elements whose class is container, and the third line of code gets the elements whose id is msg under the label. The fourth line of code shows all the connected elements in the table with context as the context.
If you are familiar with CSS, you will find these statements very familiar! By the way. Exactly. See the secret. Jquery finds the elements in the Dom object in this way. It is similar to the CSS selector.
Ii. Jquery object?
Jquery provides many convenient functions, such as each (fn), but the premise for using these functions is that you use Jquery objects. It is easy to make a Dom object a Jquery object. The following methods (only part of) are used ):
Code

The Code is as follows:


Var a = $ ("# cid ");
Var B = $ ("hello ");
Var c = document. createElement ("table ");
Var tb = $©;


3. Replace the onload of the body Tag
This Convention may be the most useful except $. The following code:
Code

The Code is as follows:


$ (Document). ready (function (){
Alert ("hello ");
}); (1)
  (2)


The above two sections of code are equivalent. But the advantage of code 1 is that performance is separated from logic. In addition, you can perform the same operation in different js files, that is, $ (document). ready (fn) can be repeated on a page without conflict. Basically, many Jqeury plug-ins use this feature. Because of this feature, multiple plug-ins are used together and will not conflict during initialization.
In any case, this Convention can be used to separate javascr ī pt from HTML. Recommended.
Iv. Event Mechanism
The most frequently used event may be the onclick of the button. Previously, I used to write onclick = "fn ()" on the input element. jquery can be used to separate the javascr transform pt code from the html code to keep the HTML clean, you can also easily bind events, or even do not know the term "events.
Code

The Code is as follows:


$ (Document). ready (function (){
$ ("# Clear"). click (function (){
Alert ("I am about to clear the table ");
});
$ ("Form [0]"). submit (validate );
});
Function validate (){
// Do some form validation
}


5. Implement the same function set
Code

The Code is as follows:


$ ("# Msg" cmd.html ();
$ ("# Msg" ).html ("hello ");


The above two lines of code call the same function. However, the results are quite different.
The first line is to return the HTML value of the specified element, and the second line is to set the character "hello" to the specified element. Most jquery functions have such features.
Vi. ajax
This is an era of ajax. The number of people who do not know about ajax is followed by one. Oh. Ajax implementation using jquery is also simple and abnormal
Code

The Code is as follows:


$. Get ("search. do", {id: 1}, rend );
Function rend (xml ){
Alert (xml );
} (1)
$. Post ("search. do", {id: 1}, rend );
Function rend (xml ){
Alert (xml );
} (2)
$ ("# Msg"). ajaxStart (function (){
This.html ("loading .... ");
}); (3)
$ ("# Msg"). ajaxSuccess (function (){
This.html ("Loading completed! ");
}); (4)


These are common methods. get and post are used in the same way. The first parameter is the url of the asynchronous request, the second parameter, and the third callback method.
Methods 3 and 4 are bound to the specified Dom object to respond to events executed by ajax. Of course, jquery's AJAX-related functions are not only these, but you can study them more if you are interested.
7. fade in and out
Code

The Code is as follows:


$ ("# Msg"). fadeIn ("fast ");
$ ("# Msg"). fadeOut ("slow ");


Yes, the above two lines of code have implemented the fade-in and fade-out of a jquery object with the id Msg respectively. It is as simple as using jquery to dynamically load notification entries like Gmail. In addition to the speed and speed, the parameters accepted by the two functions can also receive integers as the completion time of the fade-in or fade-out. The unit is MS.
8. plugin
This is also an era of plug-ins.
The jquery plug-in gives me a clear and easy way to clean. For example, to use Jtip, you only need to add Jtip to the class of your element and introduce jtip. js and its style. Other plug-ins are all-inclusive. One important reason I like jquery is that she already has many excellent and wonderful plug-ins.
Poor writing. You may not be able to see the advantages of jquery. Well, listening alone is useless. Try it out. You will find it interesting.
It's coming to an end. We will share it with you later.
Add Jquery resources:
Http://www.visualjquery.com/index.xml good API query site
Http://jquery.com/demo/thickbox/ knows lightBox, let's see how Jquery achieves the same thing
Http://jquery.com/plugins/ many cool plug-ins.
15-day tutorials for http://15daysofjquery.com/jquery
Http://jquery.org.cn/visual/cn/index.xml // good JQUERY Chinese Learning recommendation
Jquery Source
JQuery was created by John Resig, an American who has attracted many javascript experts from around the world to join the team, including Jörn Zaefferer from Germany and Stefan Petre from Romania.
JQuery is another excellent Javascr into pt framework after prototype. Its purpose is to write less, do more, write less code, and do more.
It is a lightweight js Library (only 21 k after compression), which is not suitable for other js libraries. It is compatible with CSS3 and various browsers (IE 6.0 +, FF 1.5 +, safari 2.0 +, Opera 9.0 + ).
JQuery is a fast and concise javaScript library that allows you to easily process HTML documents, events, animation effects, and provide AJAX interaction for websites.
Another major advantage of jQuery is its comprehensive documentation and detailed description of various applications. There are also many mature plug-ins to choose from.
JQuery can ensure that the user's html page is separated from the code and html content. That is to say, you don't need to insert a bunch of JavaScript code in the html to call the command. You just need to define the id.
JQuery's latest release version is 1.3.2.
Official: http://code.google.com/p/jqueryjs/downloads/list
Microsoft visual studio 2008 sp1 supports dynamic prompts for jquery. You only need to import the corresponding vsdoc script in the code page.
Related Article

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.