Dja*_*son 33 javascript css jquery
所以,目前我有一个文本输入字段,其值也是自动聚焦的.
在页面加载时,选择/突出显示该值.有没有办法我可以将光标放在值文本的末尾,而不是在javascript或CSS中突出显示它?
这是一个js小提琴,自动对焦文本的值突出显示:http://jsfiddle.net/TaGL5/
这是HTML代码: <input type="text" value="value text" autofocus />
Har*_*ram 41
这适合我
<input type="text" autofocus value="value text" onfocus="this.value = this.value;"/>Run Code Online (Sandbox Code Playgroud)
J_z*_*J_z 26
升级到@harsha的答案:我发现要使解决方案与Firefox一起工作,我们需要将临时重置值设置为"不等于值",然后将其设置回
<input type="text" autofocus value="value text" onfocus="var temp_value=this.value; this.value=''; this.value=temp_value" />Run Code Online (Sandbox Code Playgroud)
使用Jquery:
$(function() {
var input = $("#txt1");
var len = input.val().length;
input[0].focus();
input[0].setSelectionRange(len, len);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="text" id="txt1" value="Lorem" style="width:400px;" />Run Code Online (Sandbox Code Playgroud)
但是有些浏览器不支持enter code here属性,在这种情况下使用它:
$(document).ready(function(){
$("#search").focus(function(){
if (this.setSelectionRange)
{
var len = $(this).val().length;
this.setSelectionRange(len, len);
}
else
{
$(this).val($(this).val());
}
});
$("#search").focus();
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="search" type="text" value="mycurrtext" size="30" name="search" />Run Code Online (Sandbox Code Playgroud)
stackoverflow上已存在此问题:
小智 6
使用这个,它对我来说很好...
<input type="text" name="txt" value="value text" autofocus="" onfocus="this.setSelectionRange(this.value.length,this.value.length);">
Run Code Online (Sandbox Code Playgroud)
或将此行添加到您的输入元素
onfocus="this.setSelectionRange(this.value.length,this.value.length);"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39270 次 |
| 最近记录: |