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

查看:92
本文介绍了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天全站免登陆