티스토리 뷰

Web Programing Link/JQuery

jQuery on() Method

발라키르카 2018. 4. 5. 15:31
반응형

jQuery on() Method



================

용법


$(selector).on(event,childSelector,data,function,map)


selector에 event를 붙여서, 해당 이벤트가 발생하면 function 실행.


event와 function은 필수, 나머지는 선택



================

ex>


<script>

$(document).ready(function(){

    $("p").on("click", function(){

        alert("The paragraph was clicked.");

    });

});

</script>


-html-

<p>Click this paragraph.</p>


---


attach a click event to the <p> element

<P>태그 element에 click 이벤트를 붙임.



================

사례


$('input').on('change', function() {
  // Does some stuff and logs the event to the console
});

이렇게 했을때 제대로 작동하지 않을 경우,

(DOM loading과 관련된 문제인듯???)


$(document).on('change', 'input', function() {
  // Does some stuff and logs the event to the console
});
$('body').on('change', $('#id'), function() {
  // Action goes here.
});

이처럼 selector를 한단계 상위로 잡고, childSelector로 지정해서 사용.


selector는 document 보다는 특정 element를 직접 명시하는게 나음.


'Web Programing Link > JQuery' 카테고리의 다른 글

jQuery empty() Method  (0) 2018.04.05
jquery change() method  (0) 2018.04.05
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함