Lecture8 - jQuery
Lecture8 - jQuery
◦ The jQuery$(function()
way { // do stuff with the DOM });
Aspects of the DOM and
jQuery
Identification: how do I obtain a
reference to the node that I want.
Traversal: how do I move
around the DOM tree.
Node Manipulation: how do I
get or set aspects of a DOM node.
Tree Manipulation: how do I
change the structure of the page.
The DOM tree
Selecting groups of DOM
objects
name description
getElementById returns array of descendents
with the given tag, such
as "div"
getElementsByTagName returns array of descendents
with the given tag, such
as "div"
getElementsByName returns array of descendents
with the given name attribute
(mostly useful for accessing
form controls)
querySelector * returns the first element that
would be matched by the
given CSS selector string
querySelectorAll * returns an array of all
elements that would be
matched by the given CSS
selector string
jQuery node identification
// id selector
var elem = $("#myid");
// group selector
var elems = $("#myid, p");
// context selector
var elems = $("#myid < div p");
// complex selector
var elems = $("#myid < h1.special:not(.classy)");
jQuery Selectors
http://api.jquery.com/category/sel
ectors/
jQuery / DOM comparison
DOM method jQuery equivalent
getElementById("id") $("#id")
getElementsByTagName("tag") $("tag")
getElementsByName("somena $("[name='somename']")
me")
querySelector("selector") $("selector")
querySelectorAll("selector") $("selector")
Exercise
Use jQuery selectors to identify elements
with these properties in a hypothetical page:
◦ All p tags that have no children, but only if they
don't have a class of ignore
◦ Any element with the text "REPLACE_ME" in it.
◦ All div tags with a child that has a class of special
◦ All heading elements (h1, h2, h3, h4, h5, h6)
◦ Every other visible li.
Use the DOM API to target the #square and
periodically change it's position in a random
direction.
Use jQuery selectors instead of the DOM API.
Exercise
<!DOCTYPE html> <div>Some text here.</div>
<html lang="en"> <div>REPLACE_ME</div> <!-- This will be
<head>
selected. -->
<meta charset="UTF-8">
<div>
<meta name="viewport" content="width=device-
width, initial-scale=1.0"> <div class="special">I am
<title>jQuery Selectors Exercise</title> special!</div>
<script src="https://code.jquery.com/jquery- <div>Not special</div>
3.6.0.min.js"></script>
</div>
<style>
#square { <div>
width: 50px; <div>No special class here.</div>
height: 50px; </div>
background-color: blue;
position: absolute;
} <h2>Heading 2</h2>
.special { <h3>Heading 3</h3>
color: red; <h4>Heading 4</h4>
}
</style>
<ul>
</head>
<body> <li>Item 1</li>
<li style="display: none;">Item 2</li>
<h1>jQuery Selectors Exercise</h1> <li>Item 3</li>
<div>
<li>Item 4</li>
<p>This is a paragraph.</p>
<p class="ignore">This should be ignored.</p> </ul>
<p></p> <!-- This will be selected. -->
</div> <div id="square"></div>
Exercise
<script> const everyOtherVisibleLi = $
$(document).ready(function() { ('li:visible:odd');
// Select all <p> tags that have no children console.log('Every other visible <li>:
and don't have class "ignore"
', everyOtherVisibleLi);
const emptyParagraphs = $
('p:not(.ignore):empty');
console.log("Empty <p> tags: ", // Function to move the square in a
emptyParagraphs); random direction
function moveSquare() {
// Select any element with the text
const randomX = Math.random() *
"REPLACE_ME"
const replaceMeElements = $ 400; // Random X movement within the
('*:contains("REPLACE_ME")'); viewport
console.log('Elements containing const randomY = Math.random() *
"REPLACE_ME": ', replaceMeElements);
400; // Random Y movement within the
viewport
// Select all <div> tags with a child that has a
$('#square').css('transform',
class of "special"
const divWithSpecialChild = $ `translate(${randomX}px, $
('div:has(.special)'); {randomY}px)`);
console.log('Divs with a child that has class }
"special": ', divWithSpecialChild);
CS380 21
DOM tree traversal
example
<p id="foo">This is a paragraph of text with a
<a href="/path/to/another/page.html">link</a>.</p>
HTML
CS380 22
Elements vs text nodes
<div>
<p>
This is a paragraph of text with a
<a href="page.html">link</a>.
</p>
</div>
HTML
Q: How many children does the div
above have?
A: 3
◦ an element node representing the <p>
◦ two text nodes representing "\n\t"
(before/after the paragraph)
Q: How many children does the
paragraph have? The a tag? 23
jQuery traversal methods
http://api.jquery.com/category/tra
versing/
jQuery tutorials
Code Academy
http://www.codecademy.com/courses/
you-and-jquery/0?curriculum_id=4fc3
018f74258b0003001f0f#!/exercises/0
Code School:
http://www.codeschool.com/courses/jq
uery-air-first-flight