Professional Documents
Culture Documents
01. //a filter allows you to reduce the set of matched elements
02. //to those that match a given selector. In this case the query
03. //removes anything which doesn't (:not) have (:has) a child with
04. //class "selected" (.selected)
05.
06. .filter(":not(:has(.selected))")
01. //jQuery 1.4.* includes support for the has method. This method will find
02. //if a an element contains a certain other element class or whatever it is
03. //you are looking for and do anything you want to them.
04.
05. $("input").has(".email").addClass("email_icon");
01. //Look for the media‐type you wish to switch then set the href to your new style sheet
02. $('link[media='screen']').attr('href', 'Alternative.css');
01. //Where possible, pre‐fix your class names with a tag name
02. //so that jQuery doesn't have to spend more time searching
03. //for the element you're after. Also remember that anything
04. //you can do to be more specific about where the element is
05. //on your page will cut down on execution/search times
06.
07. var in_stock = $('#shopping_cart_items input.is_in_stock');
01. $('#thatdiv').replaceWith('fnuh');
01. $('#lal').append('sometext');
01. var e = $("<a>", { href: "#", class: "a‐class another‐class", title: "..." });
02. </a>
01. <a>
02. //This precision‐based approached can be useful when you use
03. //lots of similar input elements which have different types
04. var elements = $('#someid input[type=sometype][value=somevalue]').get();
05. </a>
01. <a>
02. jQuery.preloadImages = function() { for(var i = 0; i').attr('src', arguments[i]); } };
03.
04. // Usage $.preloadImages('image1.gif', '/path/to/image2.png', 'some/image3.jpg');
05. </a>
01. <a>$("p.value:contains('thetextvalue')").hide();</a>
01. <a>//This snippet will show you how to keep a default value
02. //in a text input field for when a user hasn't entered in
03. //a value to replace it
04.
05. swap_val = [];
06. $(".swap").each(function(i){
07. swap_val[i] = $(this).val();
08. $(this).focusin(function(){
09. if ($(this).val() == swap_val[i]) {
10. $(this).val("");
11. }
12. }).focusout(function(){
13. if ($.trim($(this).val()) == "") {
14. $(this).val(swap_val[i]);
15. }
16. });
17. });
18. </a>
01. <a>
02. //Here's how we used to do it in 1.3.2 using setTimeout
03. setTimeout(function() {
04. $('.mydiv').hide('blind', {}, 500)
05. }, 5000);
06.
07. //And here's how you can do it with 1.4 using the delay() feature (this is a lot like sleep)
08. $(".mydiv").delay(5000).hide('blind', {}, 500);
09. </a>
01. <a>
02. //Separate tests into modules.
03. module("Module B");
04.
05. test("some other test", function() {
06. //Specify how many assertions are expected to run within a test.
07. expect(2);
08. //A comparison assertion, equivalent to JUnit's assertEquals.
09. equals( true, false, "failing test" );
10. equals( true, true, "passing test" );
11. });
12. </a>
01. <a>
02. var arrInputValues = new Array();
03. $("input[name='table\\[\\]']").each(function(){
04. arrInputValues.push($(this).val());
05. });
06.
07. </a>
01. <a>
02. (function($) {
03. $.fn.stripHtml = function() {
04. var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
05. this.each(function() {
06. $(this).html(
07. $(this).html().replace(regexp,”")
08. );
09. });
10. return $(this);
11. }
12. })(jQuery);
13.
14. //usage:
15. $('p').stripHtml();
16.
17. </a>
01. <a>
02. // Allows chainable logging
03. // Usage: $('#someDiv').hide().log('div hidden').addClass('someClass');
04. jQuery.log = jQuery.fn.log = function (msg) {
05. if (console){
06. console.log("%s: %o", msg, this);
07. }
08. return this;
09. };
10.
11. </a>
01. <a>
02. jQuery('a.popup').live('click', function(){
03. newwindow=window.open($(this).attr('href'),'','height=200,width=150');
04. if (window.focus) {newwindow.focus()}
05. return false;
06. });
07.
08. </a>
01. <a>
02. jQuery('a.newTab').live('click', function(){
03. newwindow=window.open($(this).href);
04. jQuery(this).target = "_blank";
05. return false;
06. });
07.
08. </a>
01. <a>
02. // Rather than doing this
03. $('#nav li').click(function(){
04. $('#nav li').removeClass('active');
05. $(this).addClass('active');
06. });
07.
08. // Do this instead
09. $('#nav li').click(function(){
10. $(this).addClass('active')
11. .siblings().removeClass('active');
12. });
13. </a>
01. <a>
02. //If the value of the element matches that of the entered text
03. //it will be returned
04. $('.someClass').filter(function() {
05. return $(this).attr('value') == $('input#someId').val() ;
06. })
07. </a>
01. <a>$(document).mousemove(function(e){
02. $(document).ready(function() {
03. $().mousemove(function(e){
04. $(’#XY’).html(”X Axis : ” + e.pageX + ” | Y Axis ” + e.pageY);
05. });
06. });</a>
01. <ul><a>
02. </a><li><a href="#">Link 1</a></li>
03. <li><a href="#">Link 2</a></li>
04. <li><a href="#">Link 3</a></li>
05. <li><a href="#">Link 4</a></li>
06. </ul>
01.
02. $('#theImage').attr('src', 'image.jpg').load(function() {
03. alert('This Image Has Been Loaded');
04. });
05.
06.
07.
01.
02. //Events can be namespaced like this
03. $('input').bind('blur.validation', function(e){
04. // ...
05. });
06.
07. //The data method also accept namespaces
08. $('input').data('validation.isValid', true);
09.
10.
11.
Thanks for reading or printing out this article!. If you get a chance, remember that you can follow me at
http://www.twitter.com/addyosmani or follow my blog at AddyOsmani.com. You can also find me around
various jQuery Groups and Pages on Facebook, including jQuery Awesome. Thanks and happy coding!