jQuery设置光标在文本区域的位置
$('#input').focus(function() {
$(this).setCursorPosition(4);});更新:我修改了CMS的代码以使用jQuery,如下所示:
new function($) {
$.fn.setCursorPosition = function(pos) {
if (this.setSelectionRange) {
this.setSelectionRange(pos, pos);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
if(pos < 0) {
pos = $(this).val().length + pos;
}
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}}(jQuery);
茅侃侃
随时随地看视频慕课网APP