270 likes | 359 Views
Learn jQuery for consistent DOM manipulation, handling events, controlling styles, and improved cross-browser compatibility. Explore pre-built functionality and advanced techniques to enhance your web projects. Check the documentation for exhaustive guidance!
E N D
jQuery A Javascript Framework
Why Learn Something New? • Consistent Access and Control • DOM • Events • Styles • Forget about difference between browsers • Define object behaviors before the objects exist • lots of binding of functions and listeners • Cool, pre-made functionality (plugins)
Help http://docs.jquery.com/Main_Page • Core Functionality • Pluggins • Theming
CSS Selectors div p{} grabs all p tags within a div .info{} grabs all tags will a class of ‘info’
Getting Elements with JS • document.getElementById("ID") • Returns DOM object with and id of ID • getElementsByTagName("TAG") • Returns an array of objects, all of type TAG
jQuery to Select Elements $(“#id”) get element by ID $(“tag”) get element by tag $(".classname") get element by class returns array pointing at all HTML elements that match the select criteria
jQuery to Select Elements JS: var ele = document.getElementById("MYID"); Jquery: var ele = $(“#MYID”);
Aggregate Selects :first :last :even :odd :eq(n) $("div:first") gets all first children elements of any <div> tags
XPATH-y/DOM-y Selects • Matches for • Parents • Children • Siblings $("div p") • gets all the <p> tags within any <div> tags
Things you can do $("div p").html(‘Something new’); • Sets the innerHTML of all the Ps on the page to ‘something new’ var blah = $(“#myele").html(); • Assigns the HTML within the element with the ID ‘myele’ to the variable ‘blah’
.each $(‘p').each(function(i) { this.prepend(“<span>P #” + i + “</span>”; }); • Creates an array of all P elements on the page • Loops over that array (each array item points to the HTML element in the DOM) • Adds P# 1, P#2….P#N to the top of each paragraph
Manipulating Objects • object.html() • Returns object’s innerHTML • object.html(‘some html’) • Sets object’s html $(".alert").html(“I’m of class alert”); • Sets the html of all elements with a class of ‘alert’ to “I’m of class alert”
Controlling CSS • object.css(property, value) • Also, a bunch of shortcut properties detailed • http://docs.jquery.com/CSS $(".notice").css(‘background-color’, ‘red’); • Sets the background color of all elements belonging to the ‘notice’ class to red
AJAX $.ajax({ url: “myservice.php", success: function(data){ // executes when data from the server is finished being received alert(data); } });
AJAX… $("#news").click(function(){ $.ajax({ url: “myservice.php", success: function(data){ alert(data); } }); });
Instantiating jQuery • Link to the jQuery source Download: http://code.jquery.com/jquery-latest.js Save that file in your www or htdocs folder <script src="jquery-latest.js"></script>
Document.ready Executes after the page has downloaded all referenced assets (when the page is stable) <script> $(document).ready(function(){ //code here will executed after //the page is completely loaded }); </script>
An Example http://www.rpi.edu/~gillw3/websys/jq-example.txt
Hello World • Create an HTML page with the following in the body • <div id=“output”></div> • In the ready function • Select the div with an id of ‘output’ • Set the HTML of that element with • ‘hello world!’
Slider Plugin .slider(); creates a slider • Need to give the slider • Properties • Actions to run when events occur
Slider Plugin Add the following scripts to your page <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css rel="stylesheet" type="text/css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> You might want to cache these files locally for more consistent performance To keep jQuery light, you add only the scripts for the functions that you need
A home for the Slider • Add a 2 divs to the <body> of the page <div id=“myslider”></div> <div id=“display”></div>
Slider • Add a document.ready function to the your page (inside a <script> tag in the head of your document) • Refer to the jQuery documenation • http://docs.jquery.com/UI/Slider
Creating A Slider $("#myslider").css('width', '200px'); $("#myslider ").slider(); $("#myslider ").slider('option', 'min', 0); $("#myslider ").slider('option', 'max', 9); $("# myslider").bind('slide', function(event, ui) { $("#display").html(ui.value) });
I want my stuff to look…. • Different • jQuery Theme manager • http://jqueryui.com/themeroller/
In Class Work • Get the slider working • (such that as you slide it, it prints out the current value on the page • Print the word representation for the current slider value • Define an array (with normal js) at the top of the document