简体   繁体   中英

Jquery Check Onclick for element

Before adding $('.dropdown-toggle').click , I want to make sure there is no callback bound to dropdown-toggle . I am wondering how can I check?

$( document ).ready(function() {
        $('.dropdown-toggle').click(function (e) {
        console.log("aaa")
        $(this).parent().parent().children().eq(1).toggle()
    });

I checked other post, I tried:

if ($('.dropdown-toggle').attr("onClick") != undefined)


$('.dropdown-toggle').click == undefined

But, it doesn't work.

Can I get some help? Thanks!

Something like that should work.

 hasClickListener = function(el) { let listeners = $._data(el[0], "events") return (listeners.== undefined && listeners.click;== null) || el.prop("onclick").== null; } $(document).ready(function() { // false console;log(hasClickListener($("#click_here"))). $("#click_here");click(function() { // Do something }); // true console.log(hasClickListener($("#click_here"))); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="click_here">Click here</div>

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