PW-03 HTML DOM and Javascript
PW-03 HTML DOM and Javascript
2
HTML Tag
<br/>
tag without content
HTML Tag Attribute
gives (visual) effect to
<html>
HTML
<head> ... </head> head
<body> HTML
HTML document
... body
</body>
</html>
HTML Structure
§ HTML5 doctype declaration
§ As an identifier for web browser to treat and render the
document as HTML5 documents
§ <html> tag
§ HTML document block
§ <head>
§ Header section to put script and style definitions
§ <body>
§ Actual content of HTML document that will be rendered by
the web browser to the screen.
Document Object
Model (DOM)
HTML DOM
§ DOM stands for the Document Object Model.
§ The HTML DOM is the Document Object Model for
HTML.
§ The HTML DOM defines a standard set of objects
for HTML, and a standard way to access and
manipulate HTML objects.
§ Traversing, editing, and modifying DOM nodes
§ Editing text nodes
HTML DOM
§ The HTML DOM is a platform and language independent
API (application program interface) and can be used by
any programming language
§ The HTML DOM is used to manipulate HTML documents
§ DOM makes all components of a web page accessible
§ HTML elements
§ their attributes
§ text
§ document.createTextNode(<text>)
§ creates a new DOM text with <text>
§ the node still needs to be inserted into the DOM tree
§ <parent>.appendChild(<child>)
§ inserts <child> node behind all existing children of <parent> node
§ <parent>.insertBefore(<child>,<before>)
§ inserts <child> node before <before> child within <parent> node
§ <parent>.replaceChild(<child>,<instead>)
§ replaces <instead> child by <child> node within <parent> node
§ <parent>.removeChild(<child>)
§ removes <child> node from within <parent> node
Modifying Node Attributes
§ <node>.setAttribute(<name>,<value>)
§ sets the value of attribute <name> to <value>
§ e.g.
§ document.images[0].setAttribute("src","keiki.jpg");
http://www.w3schools.com/js/tryit.asp?filename=try_dom_change_color
HTML DOM
§ DOM Event
§ onBlur, onClick, onChange, onFocus, onKeyDown,
onKeyUp, onKeyPress, onLoad, onMouseDown, on
MouseMove, onMouseOut, onMouseOver, onSubmit, ...
§ http://science.slc.edu/~sallen/s05/examples/events.ht
ml
Cascading Style
Sheet (CSS)
Current Version CSS 3.0
What is CSS?
§ Cascading Style Sheets (CSS):
is a simple mechanism for
adding style (e.g. fonts, colors,
layouts) to Web documents.
§ Styles provide powerful
control over the presentation
of web pages.
Why CSS?
§ HTML was not meant to support styling
information
§ But browsers started supporting inline style changes to
make web look better
§ Inline styling information is problematic
§ Difficult to change
§ Lack of consistency
§ No support for different display formats
§ Bloats pages
§ No support for some styling features
27
Style Sheet Syntax
§ Internal
§ affect elements in an entire page
§ External
§ can affect multiple pages
§ Precedence
§ Local > Internal > External
Local Style Sheet
§ Example
<h1 style="color:white; background:orange; font-
weight:bold;">Internal Style Sheet Applied to
Header 1</h1>
§ Practice
§ add “text-align” property to make it centered
§ add “border” property to let it has black, 1px thick, solid border at
left, right, top, and bottom
§ Tip: use “border: <top> <right> <bottom>
<left>;” format for 4 sides; use “border-<side>: xx yy zz;”
for a particular side, where <side> can be left, right, top or
bottom. Can apply to other similar properties.
Internal Style Sheet
§ How to create?
§ Put <style> </style> tag between
<head> and </head> tags of your HTML page
§ Use type attribute to indicate the style sheet type,
usually type="text/css"
§ Put your set of style sheet rules in between <style> and
</style> tags
External Style Sheet
§ An external style sheet is simply a text-only file
that contains only CSS rules.
§ How to link to external style sheet?
<link href="URL of CSS File"
rel="stylesheet" type="text/css" />
§ Practice
§ Create a file called "mystyle.css" and do the example
in local style sheet, but as external style sheet
Selector Type
§ Tag
§ redefines the look of a specific tag
body {background-color: #000000;}
§ Class
§ can apply to any tag
.indent{margin-right:5%;margin-left: 5%;}
In HTML: <p class="indent">
§ Advanced
§ IDs, pseudo-class selectors
#myId { color: #38608A; }
a:hover { color: #FF0000; }
Values - Lenghts
§ Lengths [a number + unit identifier]
§ Unit identifier can be
em (font size of the relevant font),
ex (x-height of the relevant font),
px (pixels),
in (inches), cm (centimeter), mm,
pt (points, =1/72 in), pc (picas, 1 pc = 12 pt)
§ E.g.
h1 { margin: 0.5em }, h1 { margin: 1ex },
p { font-size: 12px }, h2 { line-height: 3cm },
h4 { font-size: 12pt }, h4 { font-size: 1pc }
Values – Percentages & URIs
§ Percentages [a number + %]
§ p { line-height: 120% }
p {
margin: 50px;
padding: 50px;
clear: right;
float: right;
border: 10px solid #0066CC;
}
text-color is #666666;
font-family is Arial, Helvetica, sans-serif;
text-indent is 20%; padding is 20px;
Positioning Properties
§ height : <length> | <percentage> | inherit
§ width : <length> | <percentage> | inherit
§ left : <length> | <percentage> | auto | inherit
§ top : <length> | <percentage> | auto | inherit
§ right : <length> | <percentage> | auto | inherit
§ bottom : <length> | <percentage> | auto | inherit
§ position : static | relative | absolute | fixed | inherit
§ left/top/right/bottom usually used when position is
specified as absolute.
More in HTML CSS
§ Positioning
§ List
§ Background
§ Animation
§ Pseudo-class
Review
§ What are the constituent elements of an HTML
document?
§ What are the differences between block-level tag and
inline-level tag?
§ What is HTML DOM?
§ What are the elements involved in CSS box-model?
§ Explain the scopes of CSS definition!
§ What does CSS capable for?
Questions?