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