尝试将其转移到textarea必须聚焦的位置,然后使用热键在特定位置插入文本...
到目前为止代码..
function insertTextAtCursor(el, text) {
var val = el.value, endIndex, range;
if (typeof el.selectionStart != "undefined" && typeof el.selectionEnd != "undefined") {
var len = el.selectionEnd - el.selectionStart;
endIndex = el.selectionEnd;
el.value = val.slice(0, el.selectionStart) + text + val.slice(endIndex);
el.selectionStart = el.selectionEnd = endIndex + (text.length - len);
el.focus();
} else if (typeof document.selection != "undefined" && typeof document.selection.createRange != "undefined") {
el.focus();
range = document.selection.createRange();
range.collapse(false);
range.text = text;
range.select();
}
}
var MouseDown = false;
document.onmousedown=function(e) {
MouseDown=true;
}
document.onmouseup=function(e) {
MouseDown=false;
}
document.onkeydown=function(e){
if(e.which == 48 && MouseDown == true) {
insertTextAtCursor(inputbox, 'Zero');
}
}
如果我把这个函数放在带有onClick事件的按钮中它可以工作......但是想要一个热键...如果我删除了这个函数并发出一个警告它有效吗?