使用JavaScript的键盘事件火灾不起作用
我想将文本框中的文本复制到剪贴板。我不想用闪光灯。 所以我尝试使用jQuery 首先,我在文本框中触发焦点事件,然后选择事件。然后我启动了ctrl+c键事件,但它不起作用 我的代码:使用JavaScript的键盘事件火灾不起作用,javascript,jquery,Javascript,Jquery,我想将文本框中的文本复制到剪贴板。我不想用闪光灯。 所以我尝试使用jQuery 首先,我在文本框中触发焦点事件,然后选择事件。然后我启动了ctrl+c键事件,但它不起作用 我的代码: <input type="button" onclick="copyText();" value="copy"></input> <input type="text" value="abc" id="texttt"></input> function copyTe
<input type="button" onclick="copyText();" value="copy"></input>
<input type="text" value="abc" id="texttt"></input>
function copyText() {
$("#texttt").focus();
$("#texttt").select();
var e = jQuery.Event("keydown");
e.ctrlKey = true; // ctrl key pressed
e.which = 67; // # c code value
$("#texttt").trigger(e);
}
$(document).ready(function () {
$("#texttt").keydown(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 67) {
if (e.ctrlKey) {
alert("ctrl+c was pressed!!");
var e = jQuery.Event("keyup");
e.which = 67;
e.ctrlKey = true;
$("#texttt").trigger(e);
}
}
});
})
函数copyText(){
$(“#texttt”).focus();
$(“#texttt”).select();
var e=jQuery.Event(“keydown”);
e、 ctrlKey=true;//按下ctrl键
e、 其中=67;/#c代码值
$(“#texttt”)。触发器(e);
}
$(文档).ready(函数(){
$(“#texttt”).keydown(函数(e){
var代码=(e.keyCode?e.keyCode:e.which);
如果(代码==67){
如果(e.ctrlKey){
警报(“按下ctrl+c!!”;
var e=jQuery.Event(“keyup”);
e、 其中=67;
e、 ctrlKey=true;
$(“#texttt”)。触发器(e);
}
}
});
})
这是到JSFIDLE的步骤 没有flash,无法将文本复制到剪贴板。这是一个太多的安全问题。浏览器不允许!IE会这样做(不是你这样做的),但是其他人都不会让你这么做。但是为什么不用jquery触发ctrl+c事件呢