Sure, use the keydown
activity.
<>strong>EDIT: 为了在文本箱中找到 cur目前的位置,你需要使用以下功能(@CMS)。 至少:
function getCaret(el) {
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint( EndToStart , re);
return rc.text.length;
}
return 0;
}
我现在回头回答:
var nobackspace = [ - ];
$(element).keydown(function(e) {
if(e.keyCode == 8) {//backspace key was pressed
var pos = getCaret(this);
if(pos > 0) pos--; //just a precaution so we don t get a negative number
if(!jQuery.inArray($(this).val().charAt(pos), nobackspace)) return false;
}
});
rel=“nofollow noreferer”>
jsFiddle example