使用JavaScript将光标放在文本输入元素中的文本末尾 [英] Use JavaScript to place cursor at end of text in text input element

查看:106
本文介绍了使用JavaScript将光标放在文本输入元素中的文本末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将焦点设置为元素后,将光标放在输入文本元素中的文本末尾的最佳方法(我假设最简单的方法)是什么?

What is the best way (and I presume simplest way) to place the cursor at the end of the text in a input text element via JavaScript - after focus has been set to the element?

推荐答案

我在IE中遇到了同样的问题(在通过RJS / prototype设置焦点之后)。
当字段已有值时,Firefox已将光标留在最后。 IE正在强制光标到文本的开头。

I faced this same issue (after setting focus through RJS/prototype) in IE. Firefox was already leaving the cursor at the end when there is already a value for the field. IE was forcing the cursor to the beginning of the text.

我到达的解决方案如下:

The solution I arrived at is as follows:

<input id="search" type="text" value="mycurrtext" size="30" 
       onfocus="this.value = this.value;" name="search"/>

这适用于IE7和FF3

This works in both IE7 and FF3

这篇关于使用JavaScript将光标放在文本输入元素中的文本末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆