J Query
J Query
Vlad Azarkhin
JavaScript was a initially introduced in Netscape 2.0B3 in Dec 1995, a.k.a. Mocha, LiveScript, Jscript,
JavaScript is a C-family, worlds worst named, extremely powerful language (not a script), totally unrelated to Java
JavaScript is a weakly typed, classless, prototype based OO language, that can also be used outside the browser. It is not a browser DOM.
programming language.
(Douglas Crockford)
Browser DOM really sucks, and this is where jQuery comes to rescue.
Introduction to jQuery
What is jQuery?
JavaScript
It was and still being developed by John Resig from Mozilla and was first announced in January 2006
Getting Started
Copy the
jquery.js
and the
jquery-vsdoc.js
<script src=jquery.js/>
///<reference path=jquery.js/>
var el = $(<div/>)
$(window).width()
$(div).hide();
$(div, $(p)).hide();
Selects document elements
(more in a moment)
jQuery(div);
jQuerys programming philosophy is: GET >> ACT $(div).hide() $(<span/>).appendTo(body) $(:button).click()
Almost every function returns jQuery, which provides a fluent programming interface and chainability:
$(div).show() .addClass(main) .html(Hello jQuery);
Chainability
jQuery Selectors
All Selector
$(*)
// find everything
Basic Selectors
By Tag:
$(div)
// <div>Hello jQuery</div>
By ID:
By Class:
$(div.main)
Combination of Selectors
// find by id + by class
$(#content, .menu)
// multiple combination
$(h1, h2, h3, div.content)
Hierarchy Selectors
$(table td)
$(tr > td) $(label + input) $(#content ~ div)
// descendants
// children // next // siblings
Visibility Filters
Attribute Filters
$(div[id])
$(div[dir=rtl]) $(div[id^=main]) $(div[id$=name]) $(a[href*=msdn])
// has attribute
// equals to // starts with // ends with // contains
Forms Selectors
$(input:checkbox)
$(input:radio) $(:button) $(:text)
// checkboxes
// radio buttons // buttons // text inputs
Forms Filters
$(input:checked)
$(input:selected) $(input:enabled) $(input:disabled)
// checked
// selected // enabled // disabled
$(select[name=ddl] option:selected).val()
SELECTORS DEMO
Document Traversal
$(div).length
Returns number of selected elements. It is the best way to check selector.
$(div).get(2) or $(div)[2]
$(div).eq(2)
Returns a 2nd jQuery element of the selection
Traversing HTML
.next(expr) .prev(expr) // next sibling // previous sibling
Find in selected
// select paragraph and then find // elements with class header inside $(p).find(.header).show();
// equivalent to: $(.header, $(p)).show();
Advanced Chaining
$(<li><span></span></li>) // li .find(span) // span .html(About Us) // span .andSelf() // span, li .addClass(menu) // span,li .end() // span .end() // li .appendTo(ul.main-menu);
HTML Manipulation
$(p).html(<div>Hello $!</div>);
Replacing Elements
$(this).html()
+ </div>); });
Deleting Elements
// remove all children $(#mainContent).empty();
Handling attributes
$(a).attr(href,home.htm); // <a href=home.htm></a>
CSS Manipulations
// get style $(div).css(background-color); // set style $(div).css(float, left); // set multiple style properties $(div).css({color:blue, padding: 1em margin-right: 0, marginLeft: 10px});
Dimensions
// get window height var winHeight = $(window).height();
// set element height $(#main).height(winHeight);
//.width() element //.innerWidth() .width() + padding //.outerWidth() .innerWidth() + border //.outerWidth(true) including margin
Positioning
// from the document $(div).offset().top; // from the parent element $(div).position().left; // scrolling position $(window).scrollTop();
Events
programming.
Uses advanced listeners for detecting.
window.onload() is a fallback.
Attach Event
// execute always $(div).bind(click, fn); // execute only once $(div).one(click, fn);
jQuery.Event object
Detaching Events
$(div).unbind(click, fn);
Events Triggering
$(div).trigger(click);
Events Helpers
// attach / trigger elem.blur(fn) / elem.blur()
elem.focus(fn) / elem.focus()
elem.click(fn) / elem.click()
elem.change(fn) / elem.change()
And many others
Preventing Bubbling
// stop bubbling, keep other handler function clickHandler(e) { e.stopPropagation(); } // stop bubbling and other handlers function clickHandler(e) { e.stopImmediatePropagation(); } // or just return false function clickHandler(e) {return false;}
Live Events
// attach live event (div).live(click, fn); // detach live event (div).die(click, fn); Currently supported events:
click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup
EVENTS DEMO
Effects
Sliding Elements
$(div).slideUp();
$(div).slideDown(fast);
$(div).slideToggle(1000);
Fading Elements
$(div).fadeIn(fast);
$(div).fadeOut(normal);
// fade to a custom opacity $(div).fadeTo (fast, 0.5);
Custom Animation
// .animate(options, duration) $(div).animate({ width: 90%, opacity: 0.5, borderWidth: 5px
}, 1000);
Chaining Animation
EFFECTS DEMO
Loading content
$(div).load(content.htm); // passing parameters $(#content).load(getcontent.aspx, {id:33, type:main});
Retrieving JS Files
$.getScript(script.js, function() { doSomeFunction(); });
AJAX DEMO
Adding Methods
// definition jQuery.fn.printLine = function(s) { return jQuery(this).each(function() { this.append(<div>+ s +</div>); }); }; // usage $(#log).printLine(Hello);
})(jQuery);
Custom Selectors
$.expr[:].test = function(o, i, m, s) { // o current object in the selection // i loop index in the stack
Data Cache
Utilities Helper functions Various Plug-ins
Where to go next
jQuery web-site: http://jquery.com
jQuery API: http://api.jquery.com
Contact me
Blog: http://blogs.microsoft.co.il/blogs/linqed
Email: vlad@netwise.co.il
Thank YOU!