javascript怎样为button按钮加上快捷键?
$(document).keydown(function(event){if(event.keyCode=='67'){$('#btnCopy').click();}})...
$(document).keydown(function(event){
if(event.keyCode == '67'){
$('#btnCopy').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '68'){
$('#btnPaste').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '67'){
$('#btnCopy').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '68'){
$('#btnPaste').click();
}
});
$(function () {
$("#btnCopy").click(function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "copy" }, function (response) {
var win = chrome.extension.getBackgroundPage();
win.data=response;
console.log(response);
});
});
});
$("#btnPaste").click(function () {
var win = chrome.extension.getBackgroundPage();
if (win.data) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "paste", data: win.data }, function (response) {
console.log(response);
});
});
}
});
});
这样哪里错了? 展开
if(event.keyCode == '67'){
$('#btnCopy').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '68'){
$('#btnPaste').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '67'){
$('#btnCopy').click();
}
});
$(document).keydown(function(event){
if(event.keyCode == '68'){
$('#btnPaste').click();
}
});
$(function () {
$("#btnCopy").click(function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "copy" }, function (response) {
var win = chrome.extension.getBackgroundPage();
win.data=response;
console.log(response);
});
});
});
$("#btnPaste").click(function () {
var win = chrome.extension.getBackgroundPage();
if (win.data) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "paste", data: win.data }, function (response) {
console.log(response);
});
});
}
});
});
这样哪里错了? 展开
1个回答
展开全部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button id='button'>按一下键盘上的A键试试:)</button>
<script>
$(document).ready(function() {
$('#button').on('click', function() {
alert('由快捷键A触发')
})
$(window).on('keypress', function(e) {
if (e.keyCode === 97) $('#button').trigger('click')
})
})
</script>
</body>
</html>
jquery的trigger用来触发事件。例如$('#button').trigger('click')
追问
有效果,谢谢Kenn。为什么97 是A??不是小键盘的1吗?
追答
你测试一下1的keycode是多少
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询