1 / 99

What’s the problem with JavaScript?

The magic of jQuery Vlad Azarkhin Senior Architect & Technologist Updated 23. february 2013 by Henrik Høltzer. What’s the problem with JavaScript?. JavaScript was a initially introduced in Netscape 2.0B3 in Dec 1995,

jason-booth
Télécharger la présentation

What’s the problem with JavaScript?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The magic of jQueryVlad AzarkhinSenior Architect & TechnologistUpdated 23. february 2013 by Henrik Høltzer

  2. What’s the problem with JavaScript?

  3. JavaScript was a initially introduced in Netscape 2.0B3 in Dec 1995, a.k.a. Mocha, LiveScript, Jscript, however, it’s official name isECMAScript

  4. JavaScript is a C-family, world’s worst named, extremely powerful language (not a script), totally unrelated to Java

  5. JavaScript is a weakly typed, classless, prototype based OO language, that can also be used outside the browser. It is not a browser DOM.

  6. The world’s most misunderstood programming language. (Douglas Crockford)

  7. Browser DOM really sucks, and this is where jQuery comes to rescue.

  8. This session is about improving your Quality of Life !

  9. Introduction to jQuery

  10. A Quality of Life by jQuery: $(“#firstName”).text(“Joe Black”); $(“button”).click(function() {alert “Clicked”;}); $(“.content”).hide(); $(“#main”).load(“content.htm”); $(“<div/>”).html(“Loading…”).appendTo(“#content”); Very compact and fluent programming model

  11. What is jQuery?

  12. jQuery is a lightweight, open-source JavaScript library that simplifiesinteraction between HTML and JavaScript

  13. It was and still being developed by John Resig from Mozilla and was first announced in January 2006

  14. It has a great community, great documentation, tons of plugins, and also adopted by Microsoft

  15. The current version is 1.9.1 (as of february 2013)

  16. Getting Started

  17. Download the latest version fromhttp://jquery.com

  18. Reference it in your markup jquery.js should contain a copy of the compressed production code • <script src=“jquery.js”/>

  19. Reference it in your JS files: … or just drag it into the file • ///<reference path=“jquery.js”/>

  20. You can also reference it from Google • <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> Or by another CDN (Content Delivery Network)

  21. jQuery Core Concepts

  22. The Magic $() function Create HTML elements on the fly • var el = $(“<div/>”)

  23. The Magic $() function Manipulate existing DOM elements • $(window).width()

  24. The Magic $() function Selects document elements (more in a moment…) • $(“div”).hide();

  25. The Magic $() function • $(function(){…}); Fired when the document is ready for programming.Better use the full syntax: • $(document).ready(function(){…});

  26. jQuery’s programming philosophy is: GET >> ACT • $(“div”).hide() • $(“<span/>”).appendTo(“body”) • $(“:button”).click()

  27. Almost every function returns jQuery, which provides a fluent programming interface and chainability: • $(“div”).show() .addClass(“main”) .html(“Hello jQuery”);

  28. Three Major Concepts of jQuery The $() function Get > Act Chainability

  29. jQuery Selectors

  30. All Selector Selectors return a pseudo-array of jQuery elements • $(“*”) // find everything

  31. Basic Selectors Yes, jQuery implements CSS Selectors! By Tag: • $(“div”) • // <div>Hello jQuery</div> By ID: • $(“#usr”) • // <span id=“usr”>John</span> By Class: • $(“.menu”) • // <ul class=“menu”>Home</ul>

  32. More Precise Selectors • $(“div.main”) // tag and class • $(“table#data”) // tag and id

  33. Combination of Selectors • // find by id + by class • $(“#content, .menu”) • // multiple combination • $(“h1, h2, h3, div.content”)

  34. Hierarchy Selectors • $(“table td”) // descendants • $(“tr > td”) // children • $(“label + input”) // next • $(“#content ~ div”) // siblings

  35. Selection Index Filters • $(“tr:first”) // first element • $(“tr:last”) // last element • $(“tr:lt(2)”) // index less than • $(“tr:gt(2)”) // index gr. than • $(“tr:eq(2)”) // index equals

  36. Visibility Filters • $(“div:visible”) // if visible • $(“div:hidden”) // if not

  37. Attribute Filters • $(“div[id]”) // has attribute • $(“div[dir=‘rtl’]”) // equals to • $(“div[id^=‘main’]”) // starts with • $(“div[id$=‘name’]”) // ends with • $(“a[href*=‘msdn’]”) // contains

  38. Forms Selectors • $(“input:checkbox”) // checkboxes • $(“input:radio”) // radio buttons • $(“:button”) // buttons • $(“:text”) // text inputs

  39. Forms Filters • $(“input:checked”) // checked • $(“input:selected”) // selected • $(“input:enabled”) // enabled • $(“input:disabled”) // disabled

  40. Find Dropdown Selected Item • <select id=“cities”> • <option value=“1”>Tel-Aviv</option> • <option value=“2” selected=“selected”>Yavne</option> • <option value=“3”>Raanana</option> • </select> • $(“#cities option:selected”).val() • $(“#cities option:selected”).text()

  41. SELECTORS DEMO

  42. Document Traversal

  43. A Selector returns a pseudo array of jQuery objects Returns number of selected elements. It is the best way to check selector. • $(“div”).length

  44. Getting a specific DOM element Returns a 2ndDOM element of the selection • $(“div”).get(2) or$(“div”)[2]

  45. Getting a specific jQuery element Returns a 2ndjQuery element of the selection • $(“div”).eq(2)

  46. each(fn) traverses every selected element calling fn() this – is a current DOM element • var sum = 0;$(“div.number”).each( function(){ sum += (+this.innerHTML); });

  47. each(fn) also passes an indexer • $(“table tr”).each( function(i){ if (i% 2) $(this).addClass(“odd”); }); $(this) – convert DOM to jQueryi - index of the current element

  48. Traversing HTML • .next(expr) // next sibling • .prev(expr) // previous sibling • .siblings(expr) // siblings • .children(expr) // children • .parent(expr) // parent

  49. Check for expression • $(“table td”).each(function() { if ($(this).is(“:first-child”)) { $(this).addClass(“firstCol”); }});

More Related