问题
I am building a webapp where a user can click on any cell in a table. On click, the text of that cell is copied and will pop up inside a disabled textarea in a fancybox, and the textarea is selected in js. The user is then invited to hit CTRL-C to copy the text that is now inside the selected textarea.
I would like to combine 2 things together when CTRL-C happens: (1) Copy the selected text (2) Close the fancybox
I can trap the CTRL-C using jquery hotkeys, but once I use that to call the fancybox close method, the event is no longer propagating. is there anyway to use both. My code is:
$(document).bind('keydown', 'ctrl+c', function(event){
$.fancybox.close(); // if this is commented out, CTRL-C works just fine
return true;
});
I have explained numerous times how this question is different from the others. I indicated in my original question that I can detect the keystrokes. The problem was how to detect that, let the action continue and also to take another action. I provided an answer below, which I figured out on my own.
回答1:
look at this solution from How to detect that Ctrl+R was pressed?
$(document).keydown(function(e) {
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
});
Edit:
<textarea>test</textarea>
<p id="test"></p>
$(document).keydown(function(e) {
if (e.keyCode == 67 && e.ctrlKey) {
/* close fancybox here */
$("#test").text("copied!"); /* optional */
}
});
来源:https://stackoverflow.com/questions/17327724/jquery-hotkeys-and-ctrl-c