Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Section A.15. Chapter 15

A.15. Chapter 15

  1. 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;
    }

  2. 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>

  3. 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>

  4. 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>


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint