By the end of today, you will be able to:
$()
Remember that jQuery is implemented using JavaScript, so there's nothing that jQuery does that you can't do in raw JavaScript.
It's more concise.
It handles browser inconsistencies and incompatibilities, so jQuery code is more likely to run correctly in all browsers.
It does cool things like those animations that we have for removing quiz answers (list items). You can try it on the items below. We'll explain that code a bit later in the course. Here is a list of some other cool things that we'll also learn later.
In the readings, the most important points we want you to understand are:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
A common mistake is to forget to load that, or to load it after your code that uses it.
$("set selector").meth1();
set selectoris the same as the CSS language:
$("nav a").css("text-decoration","none");
$("<a>").html("Search")
chainedso you can do several things with the set of elements. For example, to add a link to the nav element, we can do this:
$("<a>").html("Search").attr("href","http://www.google.com/").appendTo("nav");
.hide()
and .show()
methods.
Assume you have an HTML page with a lot of text, for example the lecture notes.
What will the statement $("p")
do?
Return the first p
element that it finds.
Return a set of all p
elements in the document.
Create a new paragraph, unattached to the document.
Create a new paragraph at the end of the body.
Which of the following statements will add a new header element to the page?
$("h2").html("November").appendTo("body");
$("<h2>").html("November").appendTo("body");
What does the other one do?
Using jQuery, how could we modify the SRC of an IMG whose ID is pic
to be "percy.jpeg"
$("#pic").attr("src","percy.jpeg");
$("#pic").src("percy.jpeg");
$("#pic").html("<img src='percy.jpeg'>")
$("#pic").setSrc("percy.jpeg")
Which of the following code excerpts will make an h1
element disappear?
$("h1").attr("display","hidden");
$("h1").display("none");
$("h1").remove();
$("h1").hide();
What's another way to make it disappear?
Suppose there are 5 H2 elements on a web page, and we execute the following code:
$("h2").css("color","red");
jQuery's method chaining does what?
What happens when the jQuery selector expression doesn't match any objects?
$ jQuery
$("h1");
$("li");
var h1 = $("h1"); h1.
Your solution might look like this:
$("h1").html("J.K. Rowling's creations"); $("h1").css("font-size","24px"); $("h1").css("color","red");
Alternatively, it might be:
$("h1").html("J.K. Rowling's creations").css("font-size","24px").css("color","red");
Or, more readably, this:
$("h1") .html("J.K. Rowling's creations") .css("font-size","24px") .css("color","red");
The latter two are equivalent, and preferable to the first because they are more efficient (though a few milli-seconds hardly matters for code like this).
The Chrome Inspector is great in many ways, but one weakness is that it's hard to have multi-line input, even for something simple like this:
function makeRed(sel) { $(sel).css("color","red"); }
It's only three lines of code, but the Chrome Inspector will complain about a syntax error when you hit "enter" after the open brace.
There are two solutions:
function makeRed(sel) { $(sel).css("color","red");}
Try typing in the makeRed
function, using either
technique, and then using it to make the H2 elements red.
addWeasley
that takes
one argument, the name of a Weasley child, and adds them to the
appropriate list.
Your solution might look like this:
$("<li>").html("Percy").appendTo("#list2"); function addWeasley(name) { $("<li>").html(name).appendTo("#list2"); } addWeasley("Fred"); addWeasley("George"); addWeasley("Ron"); addWeasley("Ginny");
Let's turn to the figure on the page.
../potterpics/hermione-granger-thumb.jpeg
) instead of
Harry's picture. It'll do that by changing value of the SRC attribute.
changePic
that takes one of these names as an argument:
The function then changes the SRC of the image appropriately. It should also change the ALT and the FIGCAPTION text.
Your solution might look like this:
function changePic(name) { $("#mainfig img") .attr("src","../potterpics/"+name+"-thumb.jpeg") .attr("alt",name); $("#mainfig figcaption").html(name); } changePic("ron-weasley");
With bonus:
$("#mainfig img") .attr("src","../potterpics/hermione-granger-thumb.jpeg") .attr("alt","Hermione Granger"); $("#mainfig figcaption").html("Hermione Granger"); function changePic(fname,lname) { var with_hyphen = fname + "-" + lname; var with_space = fname + " " + lname; $("#mainfig img") .attr("src","../potterpics/"+with_hyphen+"-thumb.jpeg") .attr("alt",with_space); $("#mainfig figcaption").html(with_space); } changePic("ron","weasley");
.hide()
method to make the figure disappear
.show()
.fadeOut()
and .fadeIn()
.
We hope that after these activities you have a good understanding of:
$()
Will be posted later, visit again after .
Quiz Questions:
1. B 2. B 3. A 4. D another way is $("h1").css("display","none"); 5. C 6. B 7. D I hate this feature of jQuery!