jQuery Click 이벤트 중복 오류

in #jquery6 years ago

jQuery를 이용해서 click 이벤트를 걸어주는 코딩을 자주 사용한다.

$("#id").on('click', function() {
    내용
}



그런데 이렇게 사용하면, 이벤트가 중복으로 걸리는 경우가 있다.
페이지가 refresh되지 않고, 한 페이지에서 여러번 스크립트가 실행하면 두배씩 이벤트가 발생한다.
그래서 아래와 같이 코드를 사용하면 이벤트 중복을 방지 할 수 있다.

$("#id").off().on('click', function() {
    내용
}



이렇게 하면 id에 해당하는 이벤트가 모두 사라진다.
Click이벤트만 제거 하고 싶다면 아래와 같이 코드를 바꾸면 된다.

$("#id").off("click").on('click', function() {
    내용
}

-fin.

Sort:  

Congratulations @jesusaddict! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 68905.09
ETH 3808.71
USDT 1.00
SBD 3.48