Download presentation
Presentation is loading. Please wait.
Published byJohnathan Cole Modified over 8 years ago
1
Copyright ©2005 Department of Computer & Information Science Advanced DHTML & DOM: Manipulating Styles with JavaScript
2
Copyright ©2005 Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to use the XHTML id attribute.Understand how to use the XHTML id attribute. Understand how to use the document.getElementById() method to access objects created by XHTML.Understand how to use the document.getElementById() method to access objects created by XHTML. continued …
3
Copyright ©2005 Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to use the JavaScript style object to update CSS styles.Understand how to use the JavaScript style object to update CSS styles. Understand how to use JavaScript to manipulate layers created by XHTML and CSS.Understand how to use JavaScript to manipulate layers created by XHTML and CSS.
4
Copyright ©2005 Department of Computer & Information Science Updating CSS using JavaScript We’ve already seen that we can dynamically change properties of objects explicitly created by HTML.We’ve already seen that we can dynamically change properties of objects explicitly created by HTML. We can also change properties for an entire group of tags established by CSS!We can also change properties for an entire group of tags established by CSS!
5
Copyright ©2005 Department of Computer & Information Science The XHTML ID Attribute To change properties using Dynamic CSS, we must first be able to uniquely identify objects created by HTML. To do this, we use the id attribute.To change properties using Dynamic CSS, we must first be able to uniquely identify objects created by HTML. To do this, we use the id attribute. We assign each tag a unique value for the id attribute. That value makes the element to which it is attached a recognizable object for JavaScript.We assign each tag a unique value for the id attribute. That value makes the element to which it is attached a recognizable object for JavaScript.
6
Copyright ©2005 Department of Computer & Information Science Specifying an ID Attribute <html><head> ID Sample ID Sample </head><body> He’s pining for the fjords! </p></body></html>
7
Copyright ©2005 Department of Computer & Information Science document.getElementById() We use document.getElementById() to establish the connection between an HTML element and JavaScript.We use document.getElementById() to establish the connection between an HTML element and JavaScript. We pass an id attribute’s value to document.getElementById () in order to make the connection. We pass the value as a string ; the method returns an object type.We pass an id attribute’s value to document.getElementById () in order to make the connection. We pass the value as a string ; the method returns an object type.
8
Copyright ©2005 Department of Computer & Information Science Open the file called javaScriptCSS_01.html
9
Copyright ©2005 Department of Computer & Information Science The style Object In the previous example, we saw our first example of updating CSS styles using JavaScript. To do so, we call upon a JavaScript attribute object called style.In the previous example, we saw our first example of updating CSS styles using JavaScript. To do so, we call upon a JavaScript attribute object called style. The style object always takes a string value!The style object always takes a string value!
10
Copyright ©2005 Department of Computer & Information Science Some CSS Equivalents CSSJavaScript font-sizefontSize font-weightfontWeight font-familyfontFamily font-stylefontStyle text-decorationtextDecoration colorcolor background-colorbackgroundColor background-imagebackgroundImage
11
Copyright ©2005 Department of Computer & Information Science The visibility Attribute The visibility attribute specifies whether a layer is visible to the user.The visibility attribute specifies whether a layer is visible to the user. The layer takes one of two values: visible or hidden.The layer takes one of two values: visible or hidden. Using JavaScript, we can adjust the visibility attribute to show or hide a layer.Using JavaScript, we can adjust the visibility attribute to show or hide a layer.
12
Copyright ©2005 Department of Computer & Information Science Open the file called javaScriptCSS_02.html
13
Copyright ©2005 Department of Computer & Information Science Open the file called javaScriptCSS_03.html
14
Copyright ©2005 Department of Computer & Information Science Moving Objects Created as Layers To move an object created as a layer using the element, we can use JavaScript to adjust the top and left attributes.To move an object created as a layer using the element, we can use JavaScript to adjust the top and left attributes. When we create a layer to move, we need to set the position attribute to “ absolute.”When we create a layer to move, we need to set the position attribute to “ absolute.”
15
Copyright ©2005 Department of Computer & Information Science Open the file called javaScriptCSS_04.html
16
Copyright ©2005 Department of Computer & Information Science Open the file called javaScriptCSS_05.html
17
Copyright ©2005 Department of Computer & Information Science Summary In order to create a reference to an object created by XHTML, we must create XHTML objects with id attributes.In order to create a reference to an object created by XHTML, we must create XHTML objects with id attributes. We can use the document.getElementById() method to create a reference to an object created by XHTML.We can use the document.getElementById() method to create a reference to an object created by XHTML. continued …
18
Copyright ©2005 Department of Computer & Information Science Summary We can update CSS styles using the JavaScript style object.We can update CSS styles using the JavaScript style object. In order to reveal or hide layers created using XHMTL and CSS, we need to dynamically update the visibility attribute.In order to reveal or hide layers created using XHMTL and CSS, we need to dynamically update the visibility attribute. To dynamically move layers created by XHTML and CSS, we need to use JavaScript to update the top & left CSS attributes.To dynamically move layers created by XHTML and CSS, we need to use JavaScript to update the top & left CSS attributes.
19
Copyright ©2005 Department of Computer & Information Science Resources Heinle, Nick & Bill Peña. Designing With JavaScript: Creating Dynamic Web Pages. Sebastopol, CA: O’Reilly & Associates, 2002.Heinle, Nick & Bill Peña. Designing With JavaScript: Creating Dynamic Web Pages. Sebastopol, CA: O’Reilly & Associates, 2002. Spain-McDuffie, Tina. JavaScript Concepts & Techniques: Programming Interactive Web Sites. Wilsonville, OR: Franklin, Beedle & Associates, 2003.Spain-McDuffie, Tina. JavaScript Concepts & Techniques: Programming Interactive Web Sites. Wilsonville, OR: Franklin, Beedle & Associates, 2003.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.