Jquery Introduction
Jquery Introduction
Developed by John Resig at Rochester Institute of Technology jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. It was released in January 2006 at BarCamp NYC by John Resig. jQuery is free, open source software Dual-licensed under the MIT License and the GNU General Public License. It s all about simplicity. Why should web developers be forced to write long, complex, book-length pieces of code when they want to create simple pieces of interaction? Current version is 1.3.2. Version 1.4 due out soon.
John Resig is a JavaScript Tool Developer for the Mozilla Corporation and the author of the book Pro JavaScript Techniques. He's also the creator and lead developer of the jQuery JavaScript library. Currently, John is located in Boston, MA. He's hard at work on his second book, Secrets of the JavaScript Ninja, due in bookstores in 2009. Microsoft and Nokia have announced plans to bundle jQuery on their platforms,[1] Microsoft adopting it initially within Visual Studio[2] for use within Microsoft's ASP.NET AJAX framework and ASP.NET MVC Framework whilst Nokia will integrate it into their Web Run-Time platform.
jQuery in Action by Bibeault & Katz, Manning jQuery Visual Quickstart Guide by Holzner, Peachpit www.jquery.com docs.jquery.com www.visualjquery.com www.Jqueryfordesigners.com www.gscottolson.com/weblog/ - cheat sheet www.javascripttoolbox.com/jquery
Class I Introduction, installation, Howdy World , Ready function, DOM, Selecting and Formatting web page elements Class II Events and Animations Class III jQuery Plugin libraries Class IV AJAX classic ASP and ASP.NET
Installation You just download the jquery1.3.2.js file and put it in your website folder Using jQuery
Visual Web Developer 2008 Express Edition Expression Web Visual Studio 2008 + SP1
"Unobtrusive JavaScript" is a general approach to the use of JavaScript in web pages. Though the term is not formally defined, its basic principles are generally understood to include: Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability) Progressive enhancement to support user agents that may not support advanced JavaScript functionality
CSS separation of style from structure Allows adding JavaScript to your web pages Advantages over just JavaScript
Much easier to use Eliminates cross-browser problems
Select DOM (Document Object Model) elements on a page one element or a group of them Set properties of DOM elements, in groups ( Find something, do something with it ) Creates, deletes, shows, hides DOM elements Defines event behavior on a page (click, mouse movement, dynamic styles, animations, dynamic content) AJAX calls
Document Object Model jQuery is DOM scripting Heirarchal structure of a web page You can add and subtract DOM elements on the fly You can change the properties and contents of DOM elements on the fly
The Document Object Model (DOM) is a cross-platform and languageindependent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM (such as its "Elements") may be addressed and manipulated within the syntax of the programming language in use. Wikipedia
jQuery() = $() $(function) The Ready handler $( selector ) Element selector expression $(element) Specify element(s) directly $( HTML ) HTML creation $.function() Execute a jQuery function $.fn.myfunc(){} Create jQuery function
Set up a basic HTML page and add jQuery Create a ready function Call a function 5 ways to specify the ready function
$(docuement).ready(function() {
$( #lstCompanyCd ).change(function(){
$( #divEE ).load( incPostBack.asp?cd= + $(this).val() , function(data){ //callback fucntion alert(data); } ); });
});
$(selector) selector:
$( #id ) id of element $( p ) tag name $( .class ) CSS class $( p.class ) <p> elements having the CSS class $( p:first ) $( p:last ) $( p:odd ) $( p:even ) $( p:eq(2) ) gets the 2nd <p> element (1 based) $( p )[1] gets the 2nd <p> element (0 based) $( p:nth-child(3)) gets the 3rd <p> element of the parent. n=even, odd too. $( p:nth-child(5n+1) ) gets the 1st element after every 5th one $( p a ) <a> elements, descended from a <p> $( p>a ) <a> elements, direct child of a <p> $( p+a ) <a> elements, directly following a <p> $( p, a ) <p> and <a> elements $( li:has(ul) ) <li> elements that have at least one <ul> descendent $( :not(p) ) all elements but <p> elements $( p:hidden ) only <p> elements that are hidden $( p:empty ) <p> elements that have no child elements
$( img [alt]) <img> elements having an alt attribute $( a [href^=http://]) <a> elements with an href attribute starting with http:// $( a [href$=.pdf]) <a> elements with an href attribute ending with .pdf $( a [href*=ntpcug]) <a> elements with an href attriute containing ntpcug
More: $( #id ).closest( td ) .children( input.myClass ).attr( checked ,true) $( .myClass ).each(function(){
$(this).attr( checked ,true) });
.each() iterate over the set .size() number of elements in set .end() reverts to the previous set .get(n) get just the nth element (0 based) .eq(n) get just the nth element (0 based) also .lt(n) & .gt(n) .slice(n,m) gets only nth to (m-1)th elements .not( p ) don t include p elements in set .add( p ) add <p> elements to set .remove() removes all the elements from the page DOM .empty() removes the contents of all the elements .filter(fn/sel) selects elements where the func returns true or sel .find(selector) selects elements meeting the selector criteria .parent() returns the parent of each element in set .children() returns all the children of each element in set .next() gets next element of each element in set .prev() gets previous element of each element in set .siblings() gets all the siblings of the current element
.css(property, value) .html() .val() (form elements) .text() .addClass( class ) .removeClass( class )
$( #target ).before( <p>Inserted before #target</p> ); $( #target ).after( <p>This is added after #target</p> ); $( #target ).append( <p>Goes inside #target, at end</p> ); $( #target ).wrap( <div></div> );
established on an element These handlers cannot be relied upon to run an any particular order When triggered, the event propagates from the top down (capture phase) or bottom up (bubble phase) IE doesn t support the capture phase
$( img ).bind( click ,function(event){alert( Howdy ;}); $( img ).bind( click ,imgclick(event));
Allows unbinding the function
$( img ).unbind( click ,imgclick()); $( img ).unbind( click ); $( img ).one( click ,imgclick(event));
Only works once
this this.id this.tagName this.attr this.src this.classname this.title this.alt this.value (for form elements)
event.target ref to element triggering event Event.target.id id of element triggering event event.currentTarget event.type type of event triggered event.data second parm in the bind() func Various mouse coordinate properties Various keystroke related properties
.stopPropagation() no bubbling .preventDefault() no <a> link, no <form> submit .trigger(eventType) does not actually trigger the event, but calls the appropriate function specified as the one tied to the eventType .click(), blur(), focus(), select(), submit() With no parameter, invokes the event handlers, like trigger does, for all the elements in the wrapped set
.click(func) .submit(func) .dblclick(func) .mouseover(func) .mouseout(func) .select(func) .click(function(){ $(this).attr( disabled ,true) });
display:true display:none first click calls func1, next click mouseover, mouseout
Opacity of picture Hover using .bind Expanding a DIV Table highlighting Expandable List Animations
What is AJAX The basic AJAX function XMLHttpRequest Initiating a request Getting the response