jQuery点击input使光标移动到最后或指定位置
你要知道面对一个 处女座的 需求者, focus()是远远不够的。 比如说“我点进去的时候光标要在最后,这样我就不用再把光标移动到最后去添加东西了。” oh,让我先撞下墙。 我们需要扩展jQuery: //光标放在最后 $("#文本框ID").textFocus();光标放在第二个字符
你要知道面对一个 处女座的 需求者, focus()是远远不够的。
比如说“我点进去的时候光标要在最后,这样我就不用再把光标移动到最后去添加东西了。”
oh,让我先撞下墙。
我们需要扩展jQuery:
//光标放在最后 $("#文本框ID").textFocus();光标放在第二个字符后面 $("#文本框ID").textFocus(2); (function($){ $.fn.textFocus=function(v){ var range,len,v=v===undefined?0:parseInt(v); this.each(function(){ if($.browser.msie){ range=this.createTextRange(); v===0?range.collapse(false):range.move("character",v); range.select(); }else{ len=this.value.length; v===0?this.setSelectionRange(len,len):this.setSelectionRange(v,v); } this.focus(); }); return this; } })(jQuery);
另一种简单的方法:
var t=$(“#”+id).val();
$(“#”+id).val(“”).focus().val(t);
建议采用第一种方法。
原文地址:jQuery点击input使光标移动到最后或指定位置, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools
