简体   繁体   中英

How to check if an elem is already listening for a function

When I add an .on() event listener twice with the same function, it fires the function twice.. ex: http://jsfiddle.net/56hxy/13/ (click the 'on' button, then click on the main green div)

How to check if an elem is already listening for a function so as to not add it twice?

if (already  $('#id').on('click',functionName)  ) 

You can unbind the event listner and then add it.

To unbind it use

$('#id').unbind('click');

Duplicate of this question? You can use the $._data method, although it's internal so probably not ideal.

Anyway it would be like: $._data($("#id")[0], "events")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM