-
jquery에서 여러개의 요소에 event를 지정하는 방법JavaScript 2023. 4. 26. 11:54
$("#특정요소").on("click",function(event){ event 실행문 }
jquery를 이용해서 특정 요소에 event 태그를 넣는 방법은 다음과 같다.
만약, 이벤트를 넣어줘야하는 특정 요소가 복수의 경우는 어떻게 할 것인가?
위의 태그를 그대로 사용하면, 복수의 특정 요소 중 첫 번째 요소에만 이벤트가 적용된다.
그렇기 때문에, 복수의 요소들에게 같은 이벤트를 부여하고 싶다면, id 가 아닌 클래스를 이용하는 것이 바람직하고 다음과 같은 방법을 이용할 수 있다.
$("#특정요소가 있는 위치").on("이벤트종류","특정요소의 클래스명",function(event){ 이벤트 실행문 }
예시)
'JavaScript' 카테고리의 다른 글
Map() 으로 임시 저장소 만들기 (0) 2023.10.20 이벤트를 바인딩하는 2가지 방법 (0) 2023.10.20 form에 이미지 파일 전송하기 (0) 2023.09.21 submit 이벤트 수정 시 유의사항 (0) 2023.04.27