CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.

Slides:



Advertisements
Similar presentations
The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
Advertisements

Cascading Style Sheets
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
Cascading Style Sheets CSS. What is it? Another file format ! its not like html Describes the looks of “selectors” in a.css file (example.css) in the.
© 2004, Robert K. Moniot Chapter 6 CSS : Cascading Style Sheets.
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
Tutorial 16 Working with Dynamic Content and Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
 Missing (or duplicate) semicolons can make the browser completely ignore the style rule.  You may add extra spaces between the properties/values to.
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
JS: Document Object Model (DOM)
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
Unobtrusive JavaScript
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CSE 154 LECTURE 18: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
CSE 154 Lecture 20: The DoM tree.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
>> More on CSS Selectors. Pre-requisite Open the file called sample.txt Copy the all the text from the file Open your browser and go to codepen.io Paste.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Week 3: Introduction to jQuery and Adv. Javascript.
JQUERY AND AJAX
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
CSE 154 LECTURE 10: MORE EVENTS. Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
JQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation,
Advanced Topics in Concurrency and Reactive Programming: HTML, CSS and DOM Trees Majeed Kassis.
Introduction to CSS: Selectors
Introduction to.
Programming Web Pages with JavaScript
>> JavaScript: Document Object Model
Unit M Programming Web Pages with
>> Introduction to CSS
CGS 3066: Web Programming and Design Spring 2017
Document Object Model (DOM): Objects and Collections
Web Programming A different world! Three main languages/tools No Java
CSE 154 Lecture 11: More Events.
A second look at JavaScript
Lecture 11: Keyboard Events
CSE 337 Lecture 10: Events.
Lecture 9: TIMERS and The DoM tree
CSE 337 Lecture 10: Events.
CSc 337 Lecture 9: The DoM tree.
Lecture 11: Keyboard Events
Web Programming and Design
Web Programming and Design
Modifying HTML attributes and CSS values
Presentation transcript:

CSE 154 LECTURE 9: THE DOM TREE

The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods for traversing this tree

DOM versus innerHTML hacking Why not just code this way? function slideClick() { document.getElementById("main").innerHTML += " A paragraph! "; } JS Imagine that the new node is more complex: ugly: bad style on many levels (e.g. JS code embedded within HTML) error-prone: must carefully distinguish " and ' can only add at beginning or end, not in middle of child list function slideClick() { document.getElementById("main").innerHTML += "<p style='color: red; " + "margin-left: 50px;' " + "onclick='myOnClick();'>" + "A paragraph! "; } JS

Creating new nodes namedescription document.createElement("tag") creates and returns a new empty DOM node representing an element of that type document.createTextNode("text")creates and returns a text node containing given text // create a new node var newHeading = document.createElement("h2"); newHeading.innerHTML = "This is a heading"; newHeading.style.color = "green"; JS merely creating a element does not add it to the page you must add the new element as a child of an existing element on the page...

Modifying the DOM tree Every DOM element object has these methods: namedescription appendChildappendChild(node)places given node at end of this node's child list insertBeforeinsertBefore(new, old)places the given new node in this node's child list just before old child removeChildremoveChild(node)removes given node from this node's child list replaceChildreplaceChild(new, old)replaces given child with new node var p = document.createElement("p"); p.innerHTML = "A paragraph!"; document.getElementById("main").appendChild(p); JS A paragraph!

Complex DOM manipulation problems How would we do each of the following in JavaScript code? Each involves modifying each one of a group of elements... When the Go button is clicked, reposition all the div s of class puzzle to random x/y locations. When the user hovers over the maze boundary, turn all maze walls red. Change every other item in the ul list with id of TAs to have a gray background.

Selecting groups of DOM objects methods in document and other DOM objects (* = HTML5): namedescription getElementsByTagNamereturns 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) querySelectorquerySelector * returns the first element that would be matched by the given CSS selector string querySelectorAllquerySelectorAll * returns an array of all elements that would be matched by the given CSS selector string

Getting all elements of a certain type highlight all paragraphs in the document: var allParas = document.querySelectorAll("p"); for (var i = 0; i < allParas.length; i++) { allParas[i].style.backgroundColor = "yellow"; } JS This is the first paragraph This is the second paragraph You get the idea... HTML

Complex selectors highlight all paragraphs inside of the section with ID "address" : // document.getElementById("address").getElementsByTagName("p") var addrParas = document.querySelectorAll("#address p"); for (var i = 0; i < addrParas.length; i++) { addrParas[i].style.backgroundColor = "yellow"; } JS This won't be returned! 1234 Street Atlanta, GA HTML

Common querySelectorAll issues many students forget to write. or # in front of a class or id // get all buttons with a class of "control" var gameButtons = document.querySelectorAll("control"); var gameButtons = document.querySelectorAll(".control"); JS querySelectorAll returns an array, not a single element; must loop over the results ( document.querySelector returns just the first element that matches, if that's what you want) // set all buttons with a class of "control" to have red text document.querySelectorAll(".gamebutton").style.color = "red"; var gameButtons = document.querySelector(".gamebutton"); for (var i = 0; i < gameButtons.length; i++) { gameButtons[i].style.color = "red"; } Q: Can I still select a group of elements using querySelectorAll even if my CSS file doesn't have any style rule for that same group? (A: Yes!)

Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick = biggerFont; }; function biggerFont() { var button = document.getElementById("clickme"); var size = parseInt(button.style.fontSize); button.style.fontSize = (size + 4) + "pt"; } JS style property lets you set any CSS style for an element problem: you cannot read existing styles with it (you can read ones you set using the DOM.style, but not ones that are set in the CSS file ) output

Accessing elements' existing styles window.getComputedStyle(element).propertyName JS function biggerFont() { // turn text yellow and make it bigger var clickMe = document.getElementById("clickme"); var size = parseInt(window.getComputedStyle(clickMe).fontSize); clickMe.style.fontSize = (size + 4) + "pt"; } JS getComputedStyle method of global window object accesses existing styles output

Common bug: incorrect usage of existing styles the following example computes e.g. "200px" "px", which would evaluate to "200px100px" var main = document.getElementById("main"); main.style.top = window.getComputedStyle(main).top "px“; // bad! JS a corrected version: main.style.top = parseInt(window.getComputedStyle(main).top) "px"; // correct JS

Getting/setting CSS classes function highlightField() { // turn text yellow and make it bigger var text = document.getElementById("text"); if (!text.className) { text.className = "highlight"; } else if (text.className.indexOf("invalid") < 0) { text.className += " highlight"; // awkward } } JS JS DOM's className property corresponds to HTML class attribute somewhat clunky when dealing with multiple space-separated classes as one big string

Getting/setting CSS classes with classList function highlightField() { // turn text yellow and make it bigger var text = document.getElementById("text"); if (!text.classList.contains("invalid")) { text.classList.add("highlight"); } } JS classList collection has methods add, remove, contains, toggle to manipulate CSS classes similar to existing className DOM property, but don't have to manually split by spaces

Removing a node from the page function slideClick() { var bullet = document.getElementById("removeme"); bullet.parentNode.removeChild(bullet); } JS odd idiom: obj.parentNode.remove(obj);

The keyword this this.fieldName // access field this.fieldName = value; // modify field this.methodName(parameters); // call method JS all JavaScript code actually runs inside of an object by default, code runs in the global window object (so this === window ) all global variables and functions you declare become part of window the this keyword refers to the current object

Event handler binding window.onload = function() { document.getElementById("textbox").onmouseout = booyah; document.getElementById("submit").onclick = booyah; }; // bound to submit button here function booyah() { // booyah knows what object it was called on this.value = "booyah"; } JS event handlers attached unobtrusively are bound to the element inside the handler, that element becomes this output

Types of DOM nodes This is a paragraph of text with a link in it. HTML element nodes (HTML tag) can have children and/or attributes text nodes (text in a block element) attribute nodes (attribute/value pair) text/attributes are children in an element node cannot have children or attributes not usually shown when drawing the DOM tree

Traversing the DOM tree manually every node's DOM object has the following properties: name(s)description firstChild, lastChildstart/end of this node's list of children childNodesarray of all this node's children nextSibling, previousSiblingneighboring nodes with the same parent parentNodethe element that contains this node complete list of DOM node properties browser incompatiblity information (IE6 sucks)browser incompatiblity information

DOM tree traversal example This is a paragraph of text with a link. HTML

Element vs. text nodes This is a paragraph of text with a link. HTML Q: How many children does the div above have? A: 3 an element node representing the two text nodes representing "\n\t" (before/after the paragraph) Q: How many children does the paragraph have? The a tag?