jsにおけるgetElementById()の使い方

4162 ワード


<html>
    <head>
        <meta charset="UTF-8">
        <title>Switch/Case handlingtitle>

        
        <script>
        window.onload=initAll;

        function initAll(){
            document.getElementById("cat").onclick=saysomething;
            document.getElementById("dog").onclick=saysomething;
            document.getElementById("tiger").onclick=saysomething;
        }

        function saysomething(){
            /*alert("init");*/
            switch(this.id){
                case "cat":
                    alert("I am a cat");
                    break;
                case "dog":
                    alert("I am a dog");
                    break;
                case "tiger":
                    alert("I am a tiger");
                    break;
                default:

            }
        }
        script>


    head>
    <body>
        <form action="#">
            <input type="button" id="cat" value="cat">
            <input type="button" id="dog" value="dog">
            <input type="button" id="tiger"   value="tiger">
        form>
    body>
html>