简体   繁体   中英

JQuery - On Click Event Handler

I have a button that I'm trying to attach an event handler to. I've tried two different ways but neither seems to work. Is there something missing?

//on ready
$( document ).ready(function() {

    //this triggers when page loads, why?
    //$("#button").on('click', alert("clicked"));   

    //this doesn't work, why?
    $("#button").on('click', function() {
        alert("clicked");
    });

});

Here's a JsFiddle link: http://jsfiddle.net/LrWvE/

.on () is added in jquery 1.7+. use .live() for jq libraries <1.7 or upgrade library to version 1.7+.

Demo using .on

Demo using .live

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