jsÖÐÔõÑùµã»÷°´Å¥ÊÂÎñ
ÔÚ javascript ÖУ¬Í¨¹ý addeventlistener ÒªÁì´¦Àí°´Å¥µã»÷ÊÂÎñ¡£Ïêϸ°ì·¨ÈçÏ£º»ñÈ¡°´Å¥ÔªËØ¡£Ìí¼ÓÊÂÎñ¼àÌýÆ÷£¬´«Èë click ÊÂÎñÀàÐͺʹ¦Àí³ÌÐòº¯Êý¡£½ç˵´¦Àí³ÌÐòº¯Êý£¬±àдÏìÓ¦µã»÷ÊÂÎñµÄ´úÂë¡£
ÔõÑùÔÚ JavaScript Öеã»÷°´Å¥ÊÂÎñ
ÔÚ JavaScript Öд¦Àí°´Å¥µã»÷ÊÂÎñºÜÊǼòÆÓ£¬Ö»ÐèҪʹÓà addEventListener ÒªÁì¼´¿É¡£ÏÂÃæÊÇÏêϸ°ì·¨£º
1. »ñÈ¡°´Å¥ÔªËØ
Ê×ÏÈ£¬ÄãÐèÒª»ñÈ¡°´Å¥ÔªËØ¡£Äã¿ÉÒÔʹÓÃÒÔÏÂÒªÁ죺
const button = document.getElementById("my-button");
µÇ¼ºó¸´ÖÆ
2. Ìí¼ÓÊÂÎñ¼àÌýÆ÷
½ÓÏÂÀ´£¬Ê¹Óà addEventListener ÒªÁìÔÚ°´Å¥ÉÏÌí¼ÓÒ»¸öÊÂÎñ¼àÌýÆ÷¡£´«ÈëµÄµÚÒ»¸ö²ÎÊýÊÇÊÂÎñÀàÐÍ£¬±¾ÀýÖÐΪ click¡£µÚ¶þ¸ö²ÎÊýÊÇ´¦Àí³ÌÐòº¯Êý£¬¸Ãº¯Êý½«ÔÚ°´Å¥±»µã»÷ʱִÐУº
button.addEventListener("click", myFunction);
µÇ¼ºó¸´ÖÆ
3. ½ç˵´¦Àí³ÌÐòº¯Êý
´¦Àí³ÌÐòº¯Êý¿ÉÒÔÊÇÈκΠJavaScript º¯Êý£¬Ëü½«ÔÚ°´Å¥±»µã»÷ʱִÐС£ÀýÈ磺
function myFunction() { // ÔÚ´Ë´¦Ìí¼ÓÒªÖ´ÐеĴúÂë }
µÇ¼ºó¸´ÖÆ
ÔÚ´¦Àí³ÌÐòº¯ÊýÖУ¬Äã¿ÉÒÔ±àдÈκÎÐëÒªµÄ´úÂ룬ÀýÈçÏÔʾ¾¯±¨¡¢·¢ËÍ AJAX ÇëÇó»ò¸üРDOM¡£
ʾÀý´úÂë
ÏÂÃæÊÇÒ»¸öÍêÕûµÄʾÀý´úÂ룬ÑÝʾÁËÔõÑùʹÓà JavaScript ´¦Àí°´Å¥µã»÷ÊÂÎñ£º
µã»÷ÎÒ <script> const button = document.getElementById("my-button"); button.addEventListener("click", function() { alert("°´Å¥±»µã»÷ÁË£¡"); }); </script>
µÇ¼ºó¸´ÖÆ
µ±Äãµã»÷°´Å¥Ê±£¬ä¯ÀÀÆ÷½«ÏÔʾһÌõ¾¯±¨ÐÂÎÅ£¬ÌáÐÑ°´Å¥±»µã»÷ÁË¡£
ÒÔÉϾÍÊÇjsÖÐÔõÑùµã»÷°´Å¥ÊÂÎñµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡