<頭><"/> <頭><">

ホームページ  >  記事  >  ウェブフロントエンド  >  JavaScriptイベントのサンプルコードを共有する

JavaScriptイベントのサンプルコードを共有する

零下一度
零下一度オリジナル
2017-06-27 09:39:411241ブラウズ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    /*
        window.onload = function () {
            alert(&#39;onload&#39;);
        }

        window.onfocus = function () {
            alert(&#39;onfocus&#39;);
        }

        window.onblur = function () {
            alert(&#39;onblur&#39;);
        }

        window.onscroll = function () {
            alert(&#39;onscroll&#39;);
        }

        window.onresize = function () {
            alert(&#39;onresize&#39;);
        }
        */
    </script>
    <script type="text/javascript">
        var msg = "";
        var myAlert = function () {
            msg += " Click Event End\n";
            alert(msg);
        }
    </script>
</head>
<body 
onclick="javascript:msg+=&#39;-->Body Event&#39;;myAlert();">
    <table border="1" 
    onclick="javascript:msg+=&#39;-->Table Event&#39;;myAlert();event.cancelBubble=true;">
    <tr 
    onclick="javascript:msg+=&#39;-->TR Event&#39;;myAlert();">
        <td 
        onclick="javascript:msg+=&#39;-->TD Event&#39;;myAlert();">
            我是单元格
            <p 
            onclick="javascript:msg+=&#39;-->P Event&#39;;myAlert();">
                我是段落
            </p>
        </td>
    </tr>
    </table>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }

        var changeSize = function (id, size, obj) {
            var inp = $(id);
            obj.value += size;
            inp.size += size;
        }

        var onclickEvent = function () {
            alert("提交内容" + $(&#39;MyButton&#39;).value);
        }
    </script>
</head>
<body>
<input type="text" id="txt" size="15"/>
<input type="button" value="加长" onclick="changeSize(&#39;txt&#39;,30,this);"/>

<center>
<br>
<p>单击“事件测试”按钮,通过匿名函数处理事件</p>
<form name="myForm" id="myForm">
  <input type="button" name="myButton" id="myButton" value="事件测试">
</form>
<script type="text/javascript">
    /*
    $("myButton").onclick = function () {
        alert(&#39;myButton onclick&#39;);
    };
    */
    //$("myButton").onclick = onclickEvent;

    /*
    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("first click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("second click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("third click Event");
    }, false);
    */

    EventUtil.addHandler(
    $("myButton")
    ,&#39;click&#39;
    , function () {
        alert("I am compatible1 click Event");
    });

    EventUtil.addHandler(
    $("myButton")
    , &#39;click&#39;
    , function () {
        alert("I am compatible2 click Event");
    });
</script>
</center>

</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }

        var changeSize = function (id, size, obj) {
            var inp = $(id);
            obj.value += size;
            inp.size += size;
        }

        var onclickEvent = function () {
            alert("提交内容" + $(&#39;MyButton&#39;).value);
        }
    </script>
</head>
<body>
<input type="text" id="txt" size="15"/>
<input type="button" value="加长" onclick="changeSize(&#39;txt&#39;,30,this);"/>

<center>
<br>
<p>单击“事件测试”按钮,通过匿名函数处理事件</p>
<form name="myForm" id="myForm">
  <input type="button" name="myButton" id="myButton" value="事件测试">
</form>
<script type="text/javascript">
    /*
    $("myButton").onclick = function () {
        alert(&#39;myButton onclick&#39;);
    };
    */
    //$("myButton").onclick = onclickEvent;

    /*
    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("first click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("second click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("third click Event");
    }, false);
    */

    EventUtil.addHandler(
    $("myButton")
    ,&#39;click&#39;
    , function () {
        alert("I am compatible1 click Event");
    });

    EventUtil.addHandler(
    $("myButton")
    , &#39;click&#39;
    , function () {
        alert("I am compatible2 click Event");
    });
</script>
</center>

</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
     .whiteStar  
     {
         float:left;
         width:64px;
         height:64px;
         background-image:url(/Images/starwhite.png)
     }
     .blueStar  
     {
         float:left;
         width:64px;
         height:64px;
         background-image:url(/Images/starblue.png)
     }
    </style>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }
    </script>
</head>
<body>
<div id="div1" class="whiteStar"></div>
<div id="div2" class="whiteStar"></div>
<div id="div3" class="whiteStar"></div>
<div id="div4" class="whiteStar"></div>
<div id="div5" class="whiteStar"></div>


<script type="text/javascript">
    //星星1
    $(&#39;div1&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div1&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
    };

    //星星2
    $(&#39;div2&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div2&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
    };

    //星星3
    $(&#39;div3&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div3&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
    };

    //星星4
    $(&#39;div4&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
        $(&#39;div4&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div4&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div4&#39;).className = &#39;whiteStar&#39;;
    };

    //星星5
    $(&#39;div5&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
        $(&#39;div4&#39;).className = &#39;blueStar&#39;;
        $(&#39;div5&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div5&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div4&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div5&#39;).className = &#39;whiteStar&#39;;
    };

</script>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script src="Scripts/KeyCodeList.js?1.1.11" type="text/javascript"></script>
</head>
<body>
    <input type="text" id="txt" />
    <script type="text/javascript">
        var objTxt = document.getElementById(&#39;txt&#39;);
        EventUtil.addHandler(objTxt, &#39;keypress&#39;, function () {
            var event = EventUtil.getEvent();
            alert(keyCodeList[event.keyCode]);
        });
    </script>
</body>
</html>

var keyCodeList = {
    65: &#39;A&#39;,
    66: &#39;B&#39;,
    67: &#39;C&#39;,
    68: &#39;D&#39;,
    69: &#39;E&#39;,
    70: &#39;F&#39;,
    71: &#39;G&#39;,
    72: &#39;H&#39;,
    73: &#39;I&#39;
    //...  
}

りー

りー

りー

以上がJavaScriptイベントのサンプルコードを共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。