如何在文本框点击事件中设置文本框中字符的第一个位置---Chrome问题
How to set first position of character in textbox in Textbox click event ---Chrome Issue
参见下面的代码 这是 HTML
<asp:TextBox ID="txtInTime" runat="server" CssClass="TextBox" Width="60px"
OnClick="fnFocus(this.id)" ></asp:TextBox>
这是JavaScript
function fnFocus(id) {
document.getElementById(id).focus();
}
上面的代码在所有浏览器中都希望铬...我希望文本框单击以在第一个位置聚焦文本框。在铬不出现在字符的第一位置..请帮忙...任何建议...??
您可以使用
setSelectionRange
执行此操作:
function fnFocus(id) {
var input = document.getElementById(id);
input.focus();
input.setSelectionRange(0, 0);
}
演示:小提琴
setSelecionRange在Chrome中对我不起作用。
试试这个X浏览器:
设置光标位置的函数,然后我们为您的文本框设置位置
$.fn.setCursorPosition = function(pos) {this.each(function(index, elem) {
if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
var range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
} }); return this; };
$('#txtInTime').setCursorPosition(0);
在你的代码中
<asp:TextBox ID="txtInTime"...... OnClick="$(this).setCursorPosition(0);"></asp:TextBox>
源
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Regex-从字符第N次出现到字符串末尾的匹配
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- 在跨度中每3个字符添加一个空格
- 如何在JavaScript中每隔第n个字符分割一个字符串
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- Javascript:表单验证getElementById仅返回第一个id元素
- 删除最后一个无效字符,而不是第一个(javascript验证)
- 获得第一个n个文本字符的网页与他们的风格等