The difference between writing jquery and directly writing JS (from a steamed bun blog)

Source: Internet
Author: User
Tags jquery library

jquery can greatly simplify the writing of JavaScript programs, I recently took the time to understand the jquery, I started the process of notes and share with you, I hope to be helpful to everyone.

To use jquery, first add a reference to the jquery library in front of the HTML code, such as:

<script language= "javascript" src= "/js/jquery.min.js" ></script>

Library files can be placed on-premises, or directly use the CDN of a well-known company, the advantage is that these large companies are more popular CDN, users visit your site before it is likely to visit other sites have been cached in the browser, so it can speed up the opening of the site. Another benefit is obvious, saving the traffic bandwidth of the website.

Provided by Google

Http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js

The official jquery

Http://code.jquery.com/jquery-1.6.min.js

I've got my own subdomain, and I'm actually jumping to Google's Cdn.

Http://cdn.akcms.com/js/jquery.js

The specific wording of the jquery code and the native JavaScript notation differ from the following when performing common operations:

1 Positioning Elements
Js
document.getElementById ("abc")

Jquery
$ ("#abc") Location by ID
$ (". ABC") through class positioning
$ ("div") positioned by tag

It is important to note that the result of JS return is this element, jquery returns the result is a JS object. The following example assumes that element ABC has been positioned.

2 Changing the contents of an element
Js
abc.innerhtml = "Test";
Jquery
abc.html ("test");

3 Show hidden elements
Js
Abc.style.display = "None";
Abc.style.display = "block";

Jquery
Abc.hide ();
Abc.show ();

Abc.toggle ();
Toggle between show and hide (2012.4.21 update)

4 getting Focus

JS and jquery are the same, both Abc.focus ();

5 Assigning a value to a form
Js
Abc.value = "Test";
Jquery
Abc.val ("test");

6 Getting the value of a form
Js
alert (Abc.value);
Jquery
Alert (Abc.val ());

7 setting elements are not available
Js
Abc.disabled = true;
Jquery
Abc.attr ("Disabled", true);

8 Modifying element styles
Js
Abc.style.fontsize=size;
Jquery
Abc.css (' font-size ', 20);

Js
Abc.classname= "Test";
Jquery
Abc.removeclass ();
Abc.addclass ("test");

9 Ajax
Js
Create objects themselves, handle browser compatibility and other messy problems, omit the table
Jquery
$.get ("abc.php?a=1&b=2", recall);
Postvalue = "a=b&c=d&abc=123";
$.post ("abc.php", Postvalue, Recall);

function recall (Result) {
alert (result);
If the JSON is returned, the following processing
result = eval (' (' + result + ') ');
alert (result);
}

10 Determine if check box is selected
Jquery
if (abc.attr ("checked") = = "Checked")
Note: Online said. attr ("checked") = = true is not actually used, the above test can be used

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.