J Query


Published on

Published in: Technology, Design
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

J Query

  1. 1. JQuery By Susheel Kumar Sharma
  2. 2. JQuery <ul><li>Powerful JavaScript library </li></ul><ul><ul><li>Simplify common JavaScript tasks </li></ul></ul><ul><ul><li>Access parts of a page </li></ul></ul><ul><ul><li>Modify the appearance of a page </li></ul></ul><ul><ul><li>Alter the content of a page </li></ul></ul><ul><ul><li>Change the user’s interaction with a page </li></ul></ul><ul><ul><li>Add animation to a page </li></ul></ul><ul><ul><li>Provide AJAX support </li></ul></ul><ul><ul><li>Abstract away browser quirks </li></ul></ul>
  3. 3. The DOM <ul><li>Document Object Model </li></ul><ul><li>jQuery is “DOM scripting” </li></ul><ul><li>Heirarchal structure of a web page </li></ul><ul><li>You can add and subtract DOM elements on the fly </li></ul><ul><li>You can change the properties and contents of DOM elements on the fly </li></ul>
  4. 4. The DOM Cont.. <ul><li>“ The Document Object Model ( DOM ) is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM (such as its &quot;Elements&quot;) may be addressed and manipulated within the syntax of the programming language in use.” </li></ul>
  5. 5. The Ready Function <ul><li>Set up a basic HTML page and add jQuery </li></ul><ul><li>Create a “ready” function </li></ul><ul><li>Call a function </li></ul><ul><ul><li>$(document).ready(function(){…};); </li></ul></ul><ul><ul><li>jquery().ready(function(){…};)‏ </li></ul></ul><ul><ul><li>jquery(function(){…};)‏ </li></ul></ul><ul><ul><li>jquery(dofunc); </li></ul></ul><ul><ul><li>$(dofunc); </li></ul></ul>
  6. 6. Selecting Elements Creating a “wrapped set” <ul><li>$(selector)‏ </li></ul><ul><li>selector: </li></ul><ul><ul><li>$(‘#id’) id of element </li></ul></ul><ul><ul><li>$(‘p’) tag name </li></ul></ul><ul><ul><li>$(‘.class’) CSS class </li></ul></ul><ul><ul><li>$(‘p.class’) <p> elements having the CSS class </li></ul></ul><ul><ul><li>$(‘p:first’) $(‘p:last’) $(‘p:odd’) $(‘p:even’)‏ </li></ul></ul><ul><ul><li>$(‘p a’) <a> elements, descended from a <p> </li></ul></ul><ul><ul><li>$(‘p>a’) <a> elements, direct child of a <p> </li></ul></ul><ul><ul><li>$(‘p+a’) <a> elements, directly following a <p> </li></ul></ul><ul><ul><li>$(‘p, a’) <p> and <a> elements </li></ul></ul><ul><ul><li>$(‘li:has(ul)’) <li> elements that have at least one <ul> descendent </li></ul></ul><ul><ul><li>$(‘:not(p)’) all elements but not <p> elements </li></ul></ul><ul><ul><li>$(‘p:hidden’) only <p> elements that are hidden </li></ul></ul><ul><li>$(‘p:empty’) <p> elements that have no child elements </li></ul><ul><li>$(‘a’[href^=http://]) <a> elements with an href attribute starting with ‘http://’ </li></ul><ul><li>$(‘a’[href$=.pdf]) <a> elements with an href attribute ending with ‘.pdf’ </li></ul><ul><li>$(‘a’[href*=ntpcug]) <a> elements with an href attriute containing ‘ntpcug’ </li></ul>
  7. 7. Filters <ul><ul><li>p:first first paragraph </li></ul></ul><ul><ul><li>li:last last list item </li></ul></ul><ul><ul><li>a:nth(3) fourth link </li></ul></ul><ul><ul><li>a:eq(3) fourth link </li></ul></ul><ul><ul><li>p:even or p:odd every other paragraph </li></ul></ul><ul><ul><li>a:gt(3) or a:lt(4) every link after the 4th or up to the fourth </li></ul></ul><ul><ul><li>a:contains(‘click’) links that contain the word click </li></ul></ul>
  8. 8. Useful Functions <ul><li>.each() iterate over the set </li></ul><ul><li>.size() number of elements in set </li></ul><ul><li>.end() reverts to the previous set </li></ul><ul><li>.get(n) get just the nth element (0 based)‏ </li></ul><ul><li>.eq(n) get just the nth element (0 based) also .lt(n) & .gt(n)‏ </li></ul><ul><li>.slice(n,m) gets only nth to (m-1)th elements </li></ul><ul><li>.not(‘p’) don’t include ‘p’ elements in set </li></ul><ul><li>.add(‘p’) add <p> elements to set </li></ul><ul><li>.remove() removes all the elements from the page DOM </li></ul><ul><li>.empty() removes the contents of all the elements </li></ul><ul><li>.filter(fn/sel) selects elements where the func returns true or sel </li></ul><ul><li>.find(selector) selects elements meeting the selector criteria </li></ul><ul><li>.parent() returns the parent of each element in set </li></ul><ul><li>.children() returns all the children of each element in set </li></ul><ul><li>.next() gets next element of each element in set </li></ul><ul><li>.prev() gets previous element of each element in set </li></ul><ul><li>.siblings() gets all the siblings of the current element </li></ul>
  9. 9. Formatting Elements <ul><li>.css(property, value)- Get the value of a style property for the first element in the set of matched elements </li></ul><ul><li>.html()- Get the HTML contents of the first element in the set of matched elements. </li></ul><ul><li>.val() (form elements)- Get the current value of the first element in the set of matched elements. </li></ul><ul><li>.text()- Get the combined text contents of each element in the set of matched elements, including their descendants. </li></ul><ul><li>.addClass(‘class’)‏ </li></ul><ul><li>.removeClass(‘class’)‏ </li></ul>
  10. 10. Add Page Elements <ul><li>$(‘#target’).before(‘<p>Inserted before #target</p>’); </li></ul><ul><li>$(‘#target’).after(‘<p>This is added after #target</p>’); </li></ul><ul><li>$(‘#target’).append(‘<p>Goes inside #target, at end</p>’); </li></ul><ul><li>$(‘#target’).wrap(‘<div></div>’); </li></ul>
  11. 11. Adding Events <ul><li>Mouseover events – bind, hover, toggle </li></ul><ul><li>Button click events </li></ul><ul><li>Keystrokes </li></ul>
  12. 12. Event Methods <ul><li>.stopPropagation()- Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. </li></ul><ul><li>.preventDefault()-i f this method is called, the default action of the event will not be triggered </li></ul><ul><li>.trigger(eventType)- Execute all handlers and behaviors attached to the matched elements for the given event type. </li></ul><ul><li>.click(), blur(), focus(), select(), submit()‏ </li></ul><ul><ul><li>With no parameter, invokes the event handlers, like trigger does, for all the elements in the wrapped set </li></ul></ul>
  13. 13. Shortcut Event Binding <ul><li>.click(func)‏ </li></ul><ul><li>.submit(func)‏ </li></ul><ul><li>.dblclick(func)‏ </li></ul><ul><li>.mouseover(func)‏ </li></ul><ul><li>.mouseout(func)‏ </li></ul><ul><li>.select(func)‏ </li></ul>
  14. 14. Ajax Perform an asynchronous HTTP (Ajax) request. AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
  15. 15. How AJAX Works?
  16. 16. function showHint(str)‏ { var xmlhttp; if (str.length==0)‏ { document.getElementById(&quot;txtHint&quot;).innerHTML=&quot;&quot;; return; } if (window.XMLHttpRequest)‏ {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;); } xmlhttp.onreadystatechange=function()‏ { if (xmlhttp.readyState==4 && xmlhttp.status==200)‏ { document.getElementById(&quot;txtHint&quot;).innerHTML=xmlhttp.responseText; } } xmlhttp.open(&quot;GET&quot;,&quot;gethint.asp?q=&quot;+str,true); xmlhttp.send(); } AJAX In Javascript
  17. 17. AJAX In JQuery Exp-1 $.ajax({ type: &quot;POST&quot;, url: &quot;some.php&quot;, data: &quot;name=John&location=Boston&quot;, success: function(msg){ alert( &quot;Data Saved: &quot; + msg ); } }); Exp - 2. $.ajax({ url: &quot;test.html&quot;, cache: false, success: function(html){ $(&quot;#results&quot;).append(html); } });