The common code of JQ collected online

Source: Internet
Author: User

1. Set the features specific to IE:

if ($.browser.msie) {

Do something ...

}

2. Use jquery instead of an element:

$ (' #thatdiv '). ReplaceWith (' Fnuh ');

3. Find the index number of an element from an unordered collection:

$ ("ul > Li"). Click (function () {

var index = $ (this). Prevall (). length;

  });

4. When creating an element, use the object literal (literal) to define the property:

var e = $ ("", {href: "#", Class: "A-class Another-class", Title: "..."});

5. use jquery to preload the image:

() {        i = 0; i < arguments.length; i++) {            $ (", arguments[i ]);        }    };

Usage $.preloadimages (' image1.gif ', '/path/to/image2.png ', ' some/image3.jpg ');

6. Set up event handlers for any element that matches the selector:

() {        $ (this). Toggleclass ("hover");
});

7. Find an OPTION element that has been selected:

$ (' #someElement '). Find (' option:selected ');

8. Hides an element that contains the text of a value:

$ ("P.value:contains (' Thetextvalue ')"). Hide ();

9. Automatically scroll to an area in the page:

JQuery.fn.autoscroll = function (selector) {$ (' html,body '). Animate ({scrolltop: $ (this). Offset (). Top},500) ;}

$ ('. Area_name '). AutoScroll ();

testing various browsers:

if ($.browser.safari)//Detect Safari

if ($.browser.msie && $.browser.version > 6)//Detect IE6 and later versions

if ($.browser.msie && $.browser.version <= 6)//Detect IE6 and previous version

  if ($.browser.mozilla && $.browser.version > ' 1.8 ')//Detect Firefox2 and later versions

11. Replace the words in the string:

var el = $ (' #id '); El.html (el.html (). Replace (/word/ig, "));

12. Disable the right-click context menu:

  $ (document). Bind (' ContextMenu ', function (e) {

return false;

});

13. Check if an element exists:

if ($ (' #someDiv '). Length) {

Exist

}

14. Use jquery to detect both right and left mouse clicks:

$ ("#someelement"). Live (' click ', Function (e) {

((!$.browser.msie && E.button = = 0) | | ($.browser.msie && E.button = = 1)) {    alert ("Left Mouse button Clicked");     (E.button = = 2) {       alert ("Right Mouse button Clicked");}    );

The common code of JQ collected online

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.