我猜原因是,所有其他字符都来自与某些字符相关联的键。但是方向键没有 ←,→ 这样的字符与之关联。他们只是有一些功能。您是否曾经使用箭头键在文本框中输入箭头?
如果你真的想用它们来输入箭头,你可以在你的 jQuery..
var element = $('#myeditor'),
elementText = element.text();
store = {
37: "←",
38: "↑",
39: "→",
40: "↓"
};
element.keydown(function( event ) {
var selection = window.getSelection().getRangeAt(0);
//getSelection gets the selected range of text, when nothing is selected,selection is empty.getRangeAt(0)makes selection from the start of selection.
var keyCode = event.which;
if( store[ keyCode ]) {
var n = document.createElement('span');
//Adds a new html element.
n.innerHTML = store[keyCode];
//Adds the arrow to the newly created html element.
selection.insertNode( n );
//insert the newly created html element.
}
});
或http://jsfiddle.net/ybPzP/10/