|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Hiding and Showing ObjectsWe will now move on to a number of real-world examples using the DOM objects to manipulate web pages. As a simple example, you can create a script that hides or shows objects within a page. As you learned in Hour 13, objects have a visibility style property that specifies whether they are currently visible within the page: Object.style.visibility="hidden"; // hides an object Object.style.visibility="visible"; // shows an object Using this property, you can create a script that hides or shows objects in either browser. Listing 14.1 shows the HTML document for a script that allows two headings to be shown or hidden. Listing 14.1. Hiding and Showing Objects
The <h1> tags in this document define headings with the identifiers head1 and head2. The <form> section defines a form with two check boxes, one for each of the headings. When a check box is modified, the onClick method is used to call the ShowHide() function. This function is defined within the <script> statements in the header. The function assigns the head1 and head2 variables to the objects for the headings, using the getElementById() method. Next, it assigns the showhead1 and showhead2 variables to the contents of the check boxes. Finally, the function uses the style.visibility attributes to set the visibility of the headings. Did you Know? The lines that set the visibility property might look a bit strange. The ? and : characters create conditional expressions, a shorthand way of handling if statements. To review conditional expressions, see Hour 7, "Controlling Flow with Conditions and Loops." Figure 14.1 shows this example in action in Internet Explorer. In the figure, the second heading's check box has been unchecked, so only the first heading is visible. Figure 14.1. The text hiding/showing example in Internet Explorer.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |