简体   繁体   中英

JS Click event doesn't work

The code:

document.getElementById("theid").onfocus=function fone(){
    document.onkeypress = function(myEvent) {
        var code = myEvent.which;
        if ((code === 13) || (code === 32)) {
            document.querySelector('.someclass').click();
        }
    }
}

I can't get what is wrong with my code...

I'm trying to call the click() when Enter or Space buttons are used while the element in focus.

EDIT

Sorry, I just got too much of learning the code for today, I guess. The case wasn't the click event. The code is correct.

You could use JQuery to get this. Please see the following fiddle:

https://jsfiddle.net/ko842xbp/

document.getElementById("theid").onfocus=function fone(){
    document.onkeypress = function(myEvent) {

        var code = myEvent.which;
        if ((code === 13) || (code === 32)) {

           $('.someclass').click();
        }
    }
}

 $('.someclass').click(function(){

 alert(".someclass was clicked")

 });

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