简体   繁体   中英

How to replace .live() with .on() when targeting the document rather than an element within

My understanding of the .on() function is that you call the function on the parent element of the target. The original jQuery I am changing is this:

 $(document).live('keydown',function(event) {
    if (event.ctrlKey==true && (event.which == '80')) { //cntrl + p
        event.preventDefault();
        printOrder();
    }
 });

So it is called directly on the document. My understanding is that I should be calling it like this:

 $(PARENT OF DOCUMENT).on('keydown', document, function(event) {
    if (event.ctrlKey==true && (event.which == '80')) { //cntrl + p
        event.preventDefault();
        printOrder();
    }
 });

I found plenty of answers on the .live() to .on() conversion, but they all referenced using a parent element. How can you reference the parent element of the document?

Just change .live to .on in your example above.

#document has no parent.

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