简体   繁体   中英

$(e.target).hasClass() Not Working

I'm trying to run certain processes on mousedown anywhere in the document, but different ones depending on whether or not certain elements are clicked. The below code isn't working. Thanks for any help!

$(document).on('mousedown',function(e) {
    if (!$(e.target).hasClass('.item')) {
        console.log('item');
    } else {
        console.log('not item);
    }
});

to hasClass() you should pass the class name, not class selector

$(document).on('mousedown',function(e) {
    if (!$(e.target).hasClass('item')) {
        console.log('item');
    } else {
        console.log('not item');
    }
});

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