我有这样的代码:
jQuery.fn.enterText = function(e){
var $cursor = $("#cursor");
if (e.keyCode == 39){
e.preventDefault();
$cursor.val("");
var nextChar = $cursor.next();
$cursor.after(nextChar);
}
};我正在尝试将#光标向右移动,但浏览器似乎不允许it....the向左箭头键工作:
if (e.keyCode == 37){
$cursor.val("");
var previousChar = $cursor.prev();
$cursor.after(previousChar);
}发布于 2011-09-27 04:41:39
您应该使用before,而不是之后:
http://jsfiddle.net/Gq5HZ/1/
if (e.keyCode == 39) {
$cursor.val("");
var nextChar = $cursor.next();
$cursor.before(nextChar);
}您正在尝试添加光标之后的元素,光标之后...它已经存在了。
发布于 2011-09-27 04:40:09
左箭头键起作用是因为您使用的是after(),所以您实际上是在移动光标元素后面的前一个字符。
我建议使用insertBefore()和insertAfter()来移动光标元素,这样您的意图就更明确了:
if (e.keyCode == 39) {
e.preventDefault();
$cursor.val("");
var nextChar = $cursor.next();
$cursor.insertAfter(nextChar);
}
if (e.keyCode == 37) {
e.preventDefault();
$cursor.val("");
var previousChar = $cursor.prev();
$cursor.insertBefore(previousChar);
}https://stackoverflow.com/questions/7560815
复制相似问题