14 useful jquery tips for sharing _jquery

Source: Internet
Author: User

1. Return jquery object instance by method

with var Somediv = $ (' #someDiv '). Hide ();  instead of var Somediv = $ (' #someDiv '); Somediv.hide ();

2. Use Find to locate

Use $ (' #someDiv '). Find (' P.someclass '). Hide (); Instead of $ (' #someDiv p.someclass '). Hide (); Because you can not fire the jquery sizzle engine, while writing the selector, try to keep your selector simple while optimizing the rightmost selector.

3. Do not misuse $ (this)

Use $ (' #someAnchor '). Click (function () {alert (this.id); }); Instead of $ (' #someAnchor '). Click (function () {Alert ($ (this). attr (' id '));

4.ready in shorthand form

With $ (function () {}); Instead of $ (document). Ready (function () {});

5. Keep your code safe

Method 1 (using noconflict)

Copy Code code as follows:

var j = jquery.noconflict ();
J (' #someDiv '). Hide ();
The line below would reference some other library ' s $ function.
$ (' Somediv '). style.display = ' None ';

Method 2 (incoming parameter jquery)
Copy Code code as follows:

(function ($) {
Within This function, $ would always refer to JQuery
}) (JQuery);

Method 3 (via Ready method)
Copy Code code as follows:

JQuery (document). Ready (function ($) {
$ refers to JQuery
});

6. Simplify code

Use each instead of for, and use the array to save the temporary variable, using the document fragment, which actually has to be noted for writing native JavaScript.

7. How to use Ajax

jquery provides a useful Ajax approach to get Getjson post Ajax

8. Access to native properties and methods
For example, the method for getting an element ID is

Copy Code code as follows:

OPTION 1–use JQuery
var id = $ (' #someAnchor '). attr (' id ');
OPTION 2–access the DOM element
var id = $ (' #someAnchor ') [0].id;
OPTION 3–use JQuery ' s Get method
var id = $ (' #someAnchor '). Get (0). ID;
OPTION 3b–don ' t pass a index to get
Anchorsarray = $ ('. Someanchors '). get ();
var thirdid = anchorsarray[2].id;

9. Use PHP to check AJAX requests

By using Xhr.setrequestheader ("X-requested-with", "XMLHttpRequest"); The server side, such as PHP, can be

Copy Code code as follows:

function Isxhr () {
Return $_server[' http_x_requested_with '] = = = ' XMLHttpRequest ';
}

To check for Ajax requests that might be used in some cases where JavaScript is disabled

The relationship between 10.Jquery and $

At the bottom of the jquery code, you can see the following code
Window.jquery = window.$ = JQuery; $ is actually a shortcut of jquery.

11. Conditional Loading jquery

Copy Code code as follows:

<!–grab Google CDN jQuery. Fall back to local if necessary–>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script>!window.jquery && document.write (' <script src= js/jquery-1.4.2.min.js ' ><\/script > ') </script>

If the CDN is not downloaded to jquery, it is read locally

12.Jquery Filters

Copy Code code as follows:

<script>
$ (' P:first '). Data (' info ', ' value '); Populates $ ' s data object to have something to work with
$.extend (
jquery.expr[":"], {
Block:function (elem) {
return $ (elem). CSS ("display") = = "Block";
},
Hasdata:function (elem) {
Return!$.isemptyobject ($ (elem). data ());
}
}
);
$ ("P:hasdata"). Text ("has data"); Grabs paras that have data attached
$ ("P:block"). Text ("are block level"); Grabs only paragraphs which have a display of "block"
</script>

Note: $.expr[":" is equivalent to $.expr.filters

13.hover method

Copy Code code as follows:

$ (' #someElement '). Hover (function () {
Here you can use the toggle method to achieve the effect of slipping and slipping
});

14. Incoming Property Object

When an element is created, Jquery1.4 can pass in a Property object

Copy Code code as follows:

$ (' </a> ', {
ID: ' Someid ',
ClassName: ' SomeClass ',
HREF: ' somepath.html '
});

Even jquery. Specified properties or events such as text, click

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.