頭><"/> <頭><">
ホームページ > 記事 > ウェブフロントエンド > JavaScriptイベントのサンプルコードを共有する
<!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('onload'); } window.onfocus = function () { alert('onfocus'); } window.onblur = function () { alert('onblur'); } window.onscroll = function () { alert('onscroll'); } window.onresize = function () { alert('onresize'); } */ </script> <script type="text/javascript"> var msg = ""; var myAlert = function () { msg += " Click Event End\n"; alert(msg); } </script> </head> <body onclick="javascript:msg+='-->Body Event';myAlert();"> <table border="1" onclick="javascript:msg+='-->Table Event';myAlert();event.cancelBubble=true;"> <tr onclick="javascript:msg+='-->TR Event';myAlert();"> <td onclick="javascript:msg+='-->TD Event';myAlert();"> 我是单元格 <p onclick="javascript:msg+='-->P Event';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("提交内容" + $('MyButton').value); } </script> </head> <body> <input type="text" id="txt" size="15"/> <input type="button" value="加长" onclick="changeSize('txt',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('myButton onclick'); }; */ //$("myButton").onclick = onclickEvent; /* $("myButton").addEventListener('click' , function () { alert("first click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("second click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("third click Event"); }, false); */ EventUtil.addHandler( $("myButton") ,'click' , function () { alert("I am compatible1 click Event"); }); EventUtil.addHandler( $("myButton") , 'click' , 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("提交内容" + $('MyButton').value); } </script> </head> <body> <input type="text" id="txt" size="15"/> <input type="button" value="加长" onclick="changeSize('txt',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('myButton onclick'); }; */ //$("myButton").onclick = onclickEvent; /* $("myButton").addEventListener('click' , function () { alert("first click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("second click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("third click Event"); }, false); */ EventUtil.addHandler( $("myButton") ,'click' , function () { alert("I am compatible1 click Event"); }); EventUtil.addHandler( $("myButton") , 'click' , 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 $('div1').onmouseover = function () { $('div1').className = 'blueStar'; }; $('div1').onmouseout = function () { $('div1').className = 'whiteStar'; }; //星星2 $('div2').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; }; $('div2').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; }; //星星3 $('div3').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; }; $('div3').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; }; //星星4 $('div4').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; $('div4').className = 'blueStar'; }; $('div4').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; $('div4').className = 'whiteStar'; }; //星星5 $('div5').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; $('div4').className = 'blueStar'; $('div5').className = 'blueStar'; }; $('div5').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; $('div4').className = 'whiteStar'; $('div5').className = 'whiteStar'; }; </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('txt'); EventUtil.addHandler(objTxt, 'keypress', function () { var event = EventUtil.getEvent(); alert(keyCodeList[event.keyCode]); }); </script> </body> </html>
var keyCodeList = { 65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I' //... }
以上がJavaScriptイベントのサンプルコードを共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。