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

Slides:



Advertisements
Similar presentations
HTML and CSS. HTML Hyper Text Markup Language Tells browser how to display text and images.
Advertisements

CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Cascading Style Sheets
Programming Club IIT Kanpur. Work environment Before you begin coding,always set up your work environment to your needs IDE Notepad++ Sublime Text 2.
CSS The basics { }. CSS Cascading Style Sheets - language used to – describe html appearance & formatting Style Sheet - file that describes – how html.
Cascading Style Sheets: Basics I450 Technology Seminar Copyright 2003, Matt Hottell.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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,
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Document Object Model (DOM) JavaScript manipulation of the DOM.
FORMATTING IN CONTEXT. FOLLOW UPS ANCHOR POINTS MUST BE UNIQUE  If you have more than one, how does it know where to go?  They can be repeated across.
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.
Today’s objectives  Element relations – tree structure  Pseudo classes  Pseudo elements.
End User Computing An Introduction to CSS Sujana Jyothi Research Lab1, Callan Building, Department of Computer Science
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
1 Pengantar Teknologi Internet W03: 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:
“Cascading Style Sheets” for styling WWW information DSC340 Mike Pangburn.
Chapter 8 Creating Style Sheets.
Web Workshop: CSS Objectives: - “What is CSS?” - Structure of CSS - How to use CSS in your webpage.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
 Missing (or duplicate) semicolons can make the browser completely ignore the style rule.  You may add extra spaces between the properties/values to.
HIGHER COMPUTING CSS. WHAT IS CSS? CSS: Cascaded Style Sheets used to separate a web site’s content(information) from its style(how it looks).
Chapter 11 Cascading Style Sheets: Part I The Web Warrior Guide to Web Design Technologies.
Cascading Style Sheets (CSS) 1.  What is CSS?  Why CSS?  How to write a CSS? 2.
Shows the entire path to the file, including the scheme, server name, the complete path, and the file name itself. Same idea of stating your full name,
Tutorial #3 Cascading Style Sheets. Review: Last Class Image sizing Pathnames Project Default Path Relative Path Absolute Path Blackboard Homework Submission.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
INTRODUCTION TO HTML5 Styling Text. Change the Font Size  You can use the font-size property to change the font size for a document’s text.  Instead.
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.
XP Dreamweaver 8.0 Tutorial 3 1 Adding Text and Formatting Text with CSS Styles.
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.
Tutorial #4 Formatting Text and Links. Tutorial #3 Review - CSS CSS format Selector { property1: value1; /* Comments */ } Embedded, In-Line, and External.
Tutorial #3 Cascading Style Sheets. Tutorial #2 Review - Anchors Links to Site DMACC Internal Links Go to Top Mail To me Local.
WRT235: Writing in Electronic Environments Basic CSS.
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.
CSE 154 Lecture 20: The DoM tree.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
>> 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.
HTML GUIDE Press F5 and then Click on the links on the left to get to the section you want Section 1: Getting Started Section 2: Moving Banner Section.
Tutorial 3 Adding and Formatting Text with CSS Styles.
Cascading Style Sheets (CSS). A style sheet is a document which describes the presentation semantics of a document written in a mark-up language such.
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.
Introduction to CSS. Why CSS? CSS Provides Efficiency in Design and Updates CSS relatively easy to use Can give you more flexibility and control Faster.
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.
Web Technologies Beginning Cascading Style Sheets (CSS) 1Copyright © Texas Education Agency, All rights reserved.
Week 3: Introduction to jQuery and Adv. Javascript.
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.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
CSS Introductions. Objectives To take control of the appearance of a Web site by creating style sheets. To use a style sheet to give all the pages of.
THE DOM.
Introduction to CSS: Selectors
Introduction to.
>> JavaScript: Document Object Model
>> Introduction to CSS
Week 4: Introduction to Javascript
Intro to CSS CS 1150 Fall 2016.
Document Object Model (DOM): Objects and Collections
Web Programming A different world! Three main languages/tools No Java
>> Dynamic CSS Selectors
Lecture 9: TIMERS and The DoM tree
CSc 337 Lecture 9: The DoM tree.
Web Programming and Design
Web Programming and Design
Presentation transcript:

The DOM tree CS380 1

The DOM tree CS380 2

Types of DOM nodes  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 CS380 3 This is a paragraph of text with a link in it. HTML

Types of DOM nodes CS380 4 This is a paragraph of text with a link in it. HTML

Traversing the DOM tree CS380 5 complete list of DOM node properties browser incompatiblity information

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

Elements vs text nodes  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? 7 This is a paragraph of text with a link. HTML

Prototype's DOM element methods 8

Prototype's DOM tree traversal methods CS380 9

Prototype's DOM tree traversal methods CS // alter siblings of "main" that do not contain "Sun" var sibs = $("main").siblings(); for (var i = 0; i < sibs.length; i++) { if (sibs[i].innerHTML.indexOf("Sun") < 0) { sibs[i].innerHTML += " Sunshine"; } } JS

Selecting groups of DOM objects  methods in document and other DOM objects for accessing descendants: CS380 11

Getting all elements of a certain type CS var allParas = document.getElementsByTagName("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

Combining with getElementById CS var addrParas = $("address").getElementsByTagName("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

Prototype's methods for selecting elements CS var gameButtons = $("game").select("button.control"); for (var i = 0; i < gameButtons.length; i++) { gameButtons[i].style.color = "yellow"; } JS Prototype adds methods to the document object (and all DOM element objects) for selecting groups of elements :

Prototype's methods for selecting elements CS apples Golden Delicious Mutsu McIntosh Ida Red exotic fruits kiwi granadilla HTML $('fruits').getElementsByClassName('yummy'); // -> [li#mutsu, …] $('exotic').getElementsByClassName('yummy'); // -> JS

Prototype's methods for selecting elements CS Apples Golden Delicious Mutsu McIntosh Ida Red An apple a day keeps the doctor away. $('apples').select('[title="yummy!"]'); // -> [h3, li#golden-delicious, li#mutsu] $('apples').select( 'p#saying', 'li[title="yummy!"]'); // $('apples').select('[title="disgusting!"]'); // JS

The $$ function 17 // hide all "announcement" paragraphs in the "news" //section var paragraphs = $$("div#news p.announcement"); for (var i = 0; i < paragraphs.length; i++) { paragraphs[i].hide(); } JS  $$ returns an array of DOM elements that match the given CSS selector  like $ but returns an array instead of a single DOM object  a shorthand for document.select  useful for applying an operation each one of a set of elements var arrayName = $$("CSS selector"); JS

Common issues with $$ 18 // get all buttons with a class of "control" var gameButtons = $$("control"); var gameButtons = $$(".control"); JS // set all buttons with a class of "control" to have red text $$(".control").style.color = "red"; var gameButtons = $$(".control"); for (var I = 0; i < gameButtons.length; i++) { gameButtons[i].style.color = "red"; } JS Q: Can I still select a group of elements using $$ even if my CSS file doesn't have any style rule for that same group? (A: Yes!)

Creating new nodes 19 // create a new node var newHeading = document.createElement("h2"); newHeading.innerHTML = "This is a heading"; newHeading.style.color = "green"; JS  merely creating a node does not add it to the page  you must add the new node as a child of an existing element on the page...

Modifying the DOM tree 20 var p = document.createElement("p"); p.innerHTML = "A paragraph!"; $("main").appendChild(p); JS CS380

Removing a node from the page 21 function slideClick() { var bullets = document.getElementsByTagName("li"); for (var i = 0; i < bullets.length; i++) { if (bullets[i].innerHTML.indexOf("children") >= 0) { bullets[i].remove(); } } JS  each DOM object has a removeChild method to remove its children from the page  Prototype adds a remove method for a node to remove itself CS380

DOM versus innerHTML hacking 22 function slideClick() { $("thisslide").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 Why not just code the previous example this way? function slideClick() { this.innerHTML += "<p style='color: red; " + "margin-left: 50px;' " + "onclick='myOnClick();'>" + "A paragraph! "; } JS

Problems with reading/changing styles 23 window.onload = function() { $("clickme").onclick = biggerFont; }; function biggerFont() { var size = parseInt($("clickme").style.fontSize); size += 4; $("clickMe").style.fontSize = size + "pt"; } JS  style property lets you set any CSS style for an element  problem: you cannot (usually) read existing styles with it CS380

Accessing styles in Prototype 24 function biggerFont() { // turn text yellow and make it bigger var size = parseInt($("clickme").getStyle("font- size")); $("clickme").style.fontSize = (size + 4) + "pt"; } JS  getStyle function added to DOM object allows accessing existing styles  addClassName, removeClassName, hasClassName manipulate CSS classes CS380

Common bug: incorrect usage of existing styles 25 this.style.top = this.getStyle("top") "px"; // bad! JS  the above example computes e.g. "200px" "px", which would evaluate to "200px100px"  a corrected version: CS380 this.style.top = parseInt(this.getStyle("top")) "px"; // correct JS

Setting CSS classes in Prototype 26 function highlightField() { // turn text yellow and make it bigger if (!$("text").hasClassName("invalid")) { $("text").addClassName("highlight"); } } JS  addClassName, removeClassName, hasClassName manipulate CSS classes  similar to existing className DOM property, but don't have to manually split by spaces CS380

Example: createElements CS Add a paragraph

Example: createElements CS window.onload = function(){ var button = $("add"); button.onclick = addParagraphClick; } function addParagraphClick(){ var paragraph = document.createElement("p"); paragraph.innerHTML = "All work and no play makes Jack a dull boy"; var area = $("paragrapharea"); area.appendChild(paragraph); } function addListClick(){ } JS

Javascript Exercises  Create a webpage with an of Homer Simpson image at the center of the page. Develop a script that prints an alert: “Duh, you are hovering!!” every time the mouse crosses over the image.  Add 5 buttons to your webpage: red, yellow, green, black, and silver. Every time you click on one of these buttons the background should take the corresponding color. CS380 29

Javascript Exercises  Add a link with the text: “CLICK ME!”. Develop a function that randomly chooses between the following websites to link your text:      CS380 30