jQuery .focus()突出显示所有预填充的文本 [英] jQuery .focus() is highlighting all pre-filled text
本文介绍了jQuery .focus()突出显示所有预填充的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML
< input id =formloginusernametype =textname =usernameplaceholder =用户名value =从数据库预填充>< / input>
JS:
< code $(document).ready(function(){
$(#formloginusername)。focus();
});
问题:
从数据库填充突出显示。我只希望光标显示在字段中,就好像用户在填充文本后点击了它一样。
谢谢!
$(document)解决方案
.ready(function(){
var $ field = $(#formloginusername),
oldVal = $ field.val();
$ field.focus()。val(' ').val(oldVal);
});
DEMO: http: //jsfiddle.net/X7Y8S/
HTML
<input id="formloginusername" type="text" name="username" placeholder="Username" value="Pre-filled-from-database"></input>
JS:
$(document).ready(function() {
$("#formloginusername").focus();
});
Problem:
The text "Pre-filled-from-database" is highlighted. I only want the cursor to show in the field as if the user had clicked it after the filled text.
Thanks!
解决方案
Here's a nifty little trick...
$(document).ready(function() {
var $field = $("#formloginusername"),
oldVal = $field.val();
$field.focus().val('').val(oldVal);
});
DEMO: http://jsfiddle.net/X7Y8S/
这篇关于jQuery .focus()突出显示所有预填充的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文