Lecture # 3 Introduction To Jquery
Lecture # 3 Introduction To Jquery
Introduction to jQuery
A Quality of Life by jQuery:
$(“#firstName”).text(“Joe Black”);
$(“.content”).hide();
$(“#main”).load(“content.htm”);
$(“<div/>”).html(“Loading…”).appendTo(“#content”);
<script src=“jquery.js”/>
///<reference path=“jquery.js”/>
var el = $(“<div/>”)
$(window).width()
$(“div”).hide();
$(function(){…});
$(document).ready(function(){…});
jQuery’s 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”);
Three Major Concepts of jQuery
Chainability
jQuery Selectors
All Selector
By Tag: $(“div”)
// <div>Hello jQuery</div>
By ID: $(“#usr”)
// <span id=“usr”>John</span>
By Class: $(“.menu”)
// <ul class=“menu”>Home</ul>
// find by id + by class
$(“#content, .menu”)
// multiple combination
$(“h1, h2, h3, div.content”)
Hierarchy Selectors
$(“div:visible”) // if visible
$(“div:hidden”) // if not
Attribute Filters
$(“input:checkbox”) // checkboxes
$(“input:radio”) // radio buttons
$(“:button”) // buttons
$(“:text”) // text inputs
Forms Filters
$(“input:checked”) // checked
$(“input:selected”) // selected
$(“input:enabled”) // enabled
$(“input:disabled”) // disabled
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()
SELECTORS DEMO
Document Traversal
A Selector returns a pseudo array of
jQuery objects
$(“div”).length
$(“div”).get(2) or $(“div”)[2]
$(“div”).eq(2)
var sum = 0;
$(“div.number”).each(
function(){
sum += (+this.innerHTML);
});
$(“table tr”).each(
function(i){
if (i % 2)
$(this).addClass(“odd”);
});
$(“table td”).each(function() {
if ($(this).is(“:first-child”)) {
$(this).addClass(“firstCol”);
}
});
Find in selected
$(“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”);
Get Part of Selected Result
$(“div”)
.slice(2, 5)
.not(“.green”)
.addClass(“middle”);
HTML Manipulation
Getting and Setting Inner Content
$(“p”).html(“<div>Hello $!</div>”);
$(“h3”).each(function(){
$(this).replaceWith(“<div>”
+ $(this).html()
+ ”</div>”);
});
Deleting Elements
// remove selection
$(“span.names”).remove();
// change position
$(“p”).remove(“:not(.red)”)
.appendTo(“#main”);
Handling attributes
$(“a”).attr(“href”,”home.htm”);
// <a href=“home.htm”>…</a>
$(“img”).attr({
“src” : “/images/smile.jpg”,
“alt” : “Smile”,
“width” : 10,
“height” : 10
});
CSS Manipulations
// get style
$(“div”).css(“background-color”);
// set style
$(“div”).css(“float”, “left”);
$(document).ready(function(){
//…
});
$(“div”).unbind(“click”, fn);
$(“div”).trigger(“click”);
// attach / trigger
elem.blur(fn) / elem.blur()
elem.focus(fn) / elem.focus()
elem.click(fn) / elem.click()
elem.change(fn) / elem.change()
$(“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);
$(“div”).hide(“slow”, function() {
alert(“The DIV is hidden”);
});
$(“div”).show(“fast”, function() {
$(this).html(“Hello jQuery”);
}); // this is a current DOM element
// .animate(options, duration)
$(“div”).animate({
width: “90%”,
opacity: 0.5,
borderWidth: “5px”
}, 1000);
Chaining Animation
$(“div”).animate({width: “90%”},100)
.animate({opacity: 0.5},200)
.animate({borderWidth: “5px”});
$(“div”)
.animate({width: “90%”},
{queue:false, duration:1000})
.animate({opacity : 0.5});
$(“div”).load(“content.htm”);
// passing parameters
$(“#content”).load(“getcontent.aspx”,
{“id”:”33”,
“type”:”main”});
Sending GET/POST requests
$.get(“test.aspx”, {id:1},
function(data){alert(data);});
$.post(“test.aspx”, {id:1},
function(data){alert(data);});
Retrieving JSON Data
$.getJSON(“users.aspx”, {id:1},
function(users)
{
alert(users[0].name);
});
Retrieving JS Files
$.getScript(“script.js”,
function()
{
doSomeFunction();
});
AJAX DEMO
Extending the Library
Adding Methods
// definition
jQuery.fn.printLine = function(s) {
return jQuery(this).each(function() {
this.append(“<div>”+ s +“</div>”);
});
};
// usage
$(“#log”).printLine(“Hello”);
(function ($) {
jQuery.fn.printLine = function(s) {
return $(this).each(function() {
this.append(“<div>”+ s +“</div>”);
});
};
})(jQuery);
Custom Selectors
$.expr[‘:’].test = function(o, i, m, s) {
// o – current object in the selection
// i – loop index in the stack
// m – meta data about your selector
// s – stack of all the elements
// return true to include the element
// return false to exclude the element
};
LIBRARY EXTENSION DEMO
More things to explore
More Functionality on every aspect
URL parameters parser
Browser and features detection
Data Cache
Utilities Helper functions
Various Plug-ins
Where to go next