jQuery 대리자() 메서드
예시
<div> 요소 안의 <p> 요소를 클릭하면 모든 <p> 요소의 배경색을 변경합니다.
$("div").delegate("p", "click", function(){
$("p").css("background-color", "pink");
});
정의 및 사용
delegate() 메서드는 버전 3.0에서 더 이상 사용되지 않습니다. 대신 on() 메서드를 사용하십시오 .
delegate() 메서드는 선택한 요소의 자식인 지정된 요소에 대해 하나 이상의 이벤트 핸들러를 연결하고 이벤트가 발생할 때 실행할 함수를 지정합니다.
delegate() 메서드를 사용하여 연결된 이벤트 핸들러는 현재 및 미래 요소(예: 스크립트로 생성된 새 요소) 모두에 대해 작동합니다.
통사론
$(selector).delegate(childSelector,event,data,function)
Parameter | Description |
---|---|
childSelector | Required. Specifies one or more child elements to attach the event handler to |
event | Required. Specifies one or more events to attach to the elements. Multiple event values are separated by space. Must be a valid event |
data | Optional. Specifies additional data to pass along to the function |
function | Required. Specifies the function to run when the event occurs |
직접 해보십시오 - 예
delegate() 메서드를 사용하여 아직 생성되지 않은 요소에 대한 이벤트 처리기를 추가하는 방법입니다.
에 데이터 전달 사용자 지정 명명된 이벤트 핸들러에 데이터를 전달하는 방법.