Download presentation
Presentation is loading. Please wait.
1
Style changes (contd.) When a user event happens in the context of some element, we may wish several aspect of the style to change For example, we may wish to change both the font size and the color To do this, we can have several style- property settings in one event handler: onmouseover = ‘this.style.color=“red”; this.style.fontSize=“50” ‘ onmouseout= ‘this.style.color=“blue”; this.style.fontSize=“20” ‘
2
Analysis of this: onmouseover=‘ this.style.color=“red”; this.style.fontSize=“50” ‘ onmouseout= ‘ this.style.color=“blue”; this.style.fontSize=“20” ‘ the overall event-handler is enclosed by apostrophes individual style settings are separated by semi-colons layout should be chosen to support human readability of the HTML specification
3
Style Property Names: CSS versus Javascript CSS property names can be written in upper or lower case -- although I encourage you to use lower-case, as in font-size Javascript property names do not contain hyphens and must be written in lower-case, except for capitals at word boundaries, as in fontSize, which corresponds to font-size borderLeftWidth, which corresponds to border- left-width
4
Example Consider the next three slides –the first one shows a document before the mouse is placed over the heading –the second one shows what the document looks like when the mouse is over the heading –the third one shows what the document looks like when the mouse is moved away again
5
Mouse not on heading
6
Mouse is on heading
7
Mouse moves off heading again
8
Event-handlers to achieve this <h1 onmouseover= 'this.style.borderStyle="solid"; this.style.borderColor="blue"; this.style.borderLeftWidth="50"; this.style.backgroundColor="black"; this.style.color="white"' onmouseout= 'this.style.borderStyle="none"; this.style.backgroundColor="white"; this.style.color="black"' > Some Subject or Other The heading above gets...
9
Multiple similar event handlers: Handling these two events for one heading required a lot of typing It would be worse if we wanted to do the same thing for many headings We need some way of abbreviating event handler specification
10
First, a motivating example: Consider the following document with several headings Consider what happens when we move the mouse over/out with respect to these headings
11
Before we move mouse over anything
12
When mouse is over 1st heading
13
After mouse is moved away
14
When mouse is over 2nd heading
15
After mouse has moved out
16
When mouse is on 3rd heading
17
How this can be done economically: We must define some new JavaScript functions
18
Using the new functions Some Subject or Other Blah blah blah. Another Subject Blah blah blah. A Third Subject Blah blah blah.
19
The new functions We have used two new functions: –highlight –restore Each time one of these functions is used, it is given one argument: the word this, which refers to the element in question
20
Function definition General form: function ( ) { } Example: function changeStyleOf(someThing) { someThing.style.color=“red”; someThing.style.fontSize=“20” } I require you to have verbs in the names of function which “do” something
21
Formal versus Actual arguments In the function definition function changeStyleOf(someThing) { someThing.style.color=“red”; someThing.style.fontSize=“20” } the word someThing is a formal argument In the event-handler onmouseover=‘changeStyleOf(this)’ the word this is an actual argument
22
Where to put function definitions They are placed in an element called a script element Such an element is delimited by two tags: and We can have lots of script elements in a document However, a script element which contains function definitions should be placed in the head of a HTML specification
23
Defining the functions used earlier function highlight(someThing) {someThing.style.borderStyle="solid"; someThing.style.borderColor="blue"; someThing.style.borderLeftWidth="50"; someThing.style.backgroundColor="black"; someThing.style.color="white" } function restore(someThing) {someThing.style.borderStyle="none"; someThing.style.backgroundColor="white"; someThing.style.color="black" }
24
Overall Document Spec. (Part I) Simple Mouse Event function highlight(someThing) {someThing.style.borderStyle="solid"; someThing.style.borderColor="blue"; someThing.style.borderLeftWidth="50"; someThing.style.backgroundColor="black"; someThing.style.color="white" } function restore(someThing) {someThing.style.borderStyle="none"; someThing.style.backgroundColor="white"; someThing.style.color="black" }
25
Overall Document Spec. (Part II) Some Subject or Other Blah blah blah. Another Subject Blah blah blah. A Third Subject Blah blah blah.
26
Event-handlers can have widespread effect An event-handler attached to one element of a document can affect any part of a document In the next example, there are event- handlers attached to three headings but they all affect only the display of the first paragraph
27
Appearance when mouse is not on any heading
28
Appearance when mouse is on first heading
29
Appearance when mouse is on second heading
30
Appearance when mouse is on third heading
31
How this is done: Only two changes from the earlier HTML specification: –the first paragraph is given an ID, which happens to be “blah1” –all the event-handlers specify that blah1 should be highlighted or restored, as the case may be –in other words, in every event-handler, blah1 is the actual argument passed to the functions
32
The body of this revised document: Some Subject or Other Blah blah blah. Another Subject Blah blah blah. A Third Subject Blah blah blah.
33
A better way of using id attributes
34
The usage of the id attribute that we have just seen was introduced in the late 1990s by Microsoft It still works in many browsers But, to future-proof your programs, you should conform to the W3C standard, –which is a little verbose
35
The body of this W3C-compatible document: Some Subject or Other Blah blah blah. Another Subject Blah blah blah. A Third Subject Blah blah blah.
36
The head of this W3C-compatible document: Simple Mouse Event function highlight(someString) {var someThing=document.getElementById(someString); someThing.style.borderStyle="solid"; someThing.style.borderColor="blue"; someThing.style.borderLeftWidth="50"; someThing.style.backgroundColor="black"; someThing.style.color="white" } function restore(someString) {var someThing=document.getElementById(someString); someThing.style.borderStyle="none"; someThing.style.backgroundColor="white"; someThing.style.color="black" }
37
Another example: events on headings affecting their related paragraphs Some Subject or Other Blah blah blah. Another Subject Blah blah blah. A Third Subject Blah blah blah.
38
Mouse on first heading
39
Mouse on second heading
40
Mouse on third heading
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.