먼저 JS의 이벤트를 추가하는 일반적인 방법을 소개합니다. 특정 내용은 다음과 같습니다
<! docType html> <html> <head> <meta charset = "utf-8"> </title> </head> <body> <p id = "p1"> <p id = "p1"> 테스트 추가 이벤트 : addeventListener를 사용합니다 <br>이 p 태그를 클릭하고 2 개의 팝업 이벤트> <test1 (test1) } 함수 test2 () {alert ( "test2"); } // 이벤트 일반 메소드 함수 addevent (element, e, fn) {// firefox는 addeventListener를 사용하여 이벤트를 추가합니다. } // 즉, 첨부 이벤트를 사용하여 이벤트를 추가하여 {element.attachevent ( "on"+e, fn); }} window.onload = function () {var 요소 = document.getElementById ( "p1"); addevent (요소, "클릭", test1); addevent (요소, "클릭", test2); } </script> </body> </html>JS 이벤트를 바인딩하는 일반적인 방법 :
이벤트 바인딩 방법 : 이벤트 속성으로 이벤트 기능 바인드
이점:
1. 행동의 분리를 완료하십시오
2. 함수가 on *** 속성으로 나타나기 때문에 관련된 객체를 작동하는 것이 편리합니다.이를 사용하는 객체를 직접 참조 할 수 있습니다.
3. 읽기 쉬운 이벤트 개체. 이벤트가 트리거되면 시스템은 이벤트 객체를 이벤트 기능으로 자동 전달하고 그 중 하나가 전달됩니다.
<? xml version = "1.0"encoding = "utf-8"?> <! doctype html public "-// w3c // dtd xhtml 1.0 Transitional // en" "http://www.w3.org/xhtml1/dtd/xhtml1-ransitional.dtd"> <httml "> xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type"content = "text/html; charset = utf-8"/> <title> js 이벤트 바인딩 </title> <script type = "text/javascript"{function (window.onload). k = document.getElementById ( 'k'). onclick = function (event) {var jj = document.getElementById ( 'jj'); jj.style.top = event.clientx+'px'; jj.style.left = event.clienty+'px'; }} </script> <tyle> #k {너비 : 60px; 높이 : 80px; 배경색 :#80ffff;} #jj {width : 60px; 높이 : 80px; 배경 색; 배경색; Z-index : 1000; 위치 : 절대;} </style> </head> <body> <div id = "k"> </div> <div id = "jj"> </div> </html> </html>위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.