相關連結推薦:http://tw.newtonstudio.com/?p=59

 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>02eventHandlerAttachAdd.html</title>

    <script>

        window.onload = function () {

            if (document.addEventListener) {//如果有此方法就執行這個方法

                //true向下、false向上

                //一律先向到底下再向上

                document.addEventListener("click", clickDoc, false);

                document.getElementById("iddiv1").addEventListener("click", clickDiv1, true);

                document.getElementById("iddiv2").addEventListener("click", clickDiv2, true);

            }

                //TFTD21

                //TFFD21

                //FTT12D

            else if (document.attachEvent) {//如果沒有上面的方法,例如IE8,那就

                document.attachEvent("onclick", clickDoc);

                document.getElementById("iddiv1").attachEvent("onclick", clickDiv1);

                document.getElementById("iddiv2").attachEvent("onclick", clickDiv2);

            }

 

        }

 

       

        function clickDoc() {

            alert("document");

        }

 

        function clickDiv1() {

            alert("Div1");

        }

 

        function clickDiv2() {

            alert("Div2");

        }

 

    </script>

</head>

<body>

    <div id="iddiv1">

                     div1

                     <div id="iddiv2">div2</div>

           </div>

</body>

</html>

 

乙方 發表在 痞客邦 留言(0) 人氣()