Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Here’s an example page; there are many ways to complete this exercise correctly:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>CSS</title> <link href="exercise1.css" rel="stylesheet" type="text/css"> </head> <body> <h1 id="h1element">The Title</h1> <p id="firstelement">The first element.</p> <p id="secondelement">The second element.</p> </body> </html>
Here is the stylesheet exercise1.css:
#h1element {
background-color: #abacab;
}
#firstelement {
color: red;
}
#secondelement {
color: blue;
}This code changes the element named firstelement so that its font color is blue:
<script type="text/javascript">
var element1 = document.getElementById("firstelement");
element1.style.color = "#0000FF";
</script>This code hides all the <p> elements using the Cascading Style Sheets (CSS) visibility property:
<script type="text/javascript">
var pelements = document.getElementsByTagName("p");
var pelmLength = pelements.length;
for (var i = 0; i < pelmLength; i++) {
pelements[i].style.visibility = "hidden";
}
</script>This code shows the visibility setting both before and after it has been set within the script. When you run the code, notice that the alert is empty before the property is set.
<script type="text/javascript">
var pelements = document.getElementsByTagName("p");
var pelmLength = pelements.length;
for (var i = 0; i < pelmLength; i++) {
alert(pelements[i].style.visibility);
pelements[i].style.visibility = "hidden";
alert(pelements[i].style.visibility);
}
</script>