jquery 输入全选焦点 [英] jquery input select all on focus

查看:23
本文介绍了jquery 输入全选焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户专注于该字段时,我正在使用此代码尝试选择该字段中的所有文本.发生的情况是,它选择所有一秒钟,然后取消选择,并且键入光标留在我单击的位置...

I'm using this code to try and select all of the text in the field when a user focuses on the field. What happens is, it selects all for a second, then its unselected and the typing cursor is left where I clicked...

$("input[type=text]").focus(function() {
   $(this).select();
});

我希望所有内容都保持选中状态.

I want it all to remain selected.

推荐答案

尝试使用 click 而不是 focus.它似乎适用于鼠标和按键事件(至少在 Chrome/Mac 上):

Try using click instead of focus. It seems to work for both mouse and key events (at least on Chrome/Mac):

jQuery <1.7 版:

jQuery < version 1.7:

$("input[type='text']").click(function () {
   $(this).select();
});

jQuery 1.7+ 版:

jQuery version 1.7+:

$("input[type='text']").on("click", function () {
   $(this).select();
});

这是一个演示

这篇关于jquery 输入全选焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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