jquery 输入全选焦点
当用户关注该字段时,我使用此代码尝试选择该字段中的所有文本。发生的情况是,它选择了所有内容一秒钟,然后取消选择,并且打字光标留在我单击的位置...
$("input[type=text]").focus(function() {
$(this).select();
});
我希望所有内容都保持选中状态。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
尝试使用
click
而不是focus
。它似乎适用于鼠标和按键事件(至少在 Chrome/Mac 上):jQuery <版本 1.7:
jQuery 版本 1.7+:
这是一个演示
Try using
click
instead offocus
. It seems to work for both mouse and key events (at least on Chrome/Mac):jQuery < version 1.7:
jQuery version 1.7+:
Here is a demo
我认为发生的情况是这样的:
解决方法可能是异步调用 select() ,以便它在 focus() 之后完全运行:
I think that what happens is this:
A workaround may be calling the select() asynchronously, so that it runs completely after focus():
我认为这是更好的解决方案。与简单地在 onclick 事件中选择不同,它不会阻止使用鼠标选择/编辑文本。它适用于包括 IE8 在内的主要渲染引擎。
http://jsfiddle.net/25Mab/9/
I think this is better solution. Unlike simply selecting in onclick event, it doesn't prevent selecting/editing text with mouse. It works with major rendering engines including IE8.
http://jsfiddle.net/25Mab/9/
这里有一些不错的答案,@user2072367 是我最喜欢的,但是当您通过选项卡而不是通过单击聚焦时,它会产生意想不到的结果。 (意想不到的结果:要通过选项卡获得焦点后正常选择文本,您必须再单击一次)
这个小提琴修复了这个小问题bug 并另外将 $(this) 存储在变量中以避免冗余 DOM 选择。一探究竟! (:
在 IE > 8 下测试
There are some decent answers here and @user2072367 's is my favorite, but it has an unexpected result when you focus via tab rather than via click. ( unexpected result: to select text normally after focus via tab, you must click one additional time )
This fiddle fixes that small bug and additionally stores $(this) in a variable to avoid redundant DOM selection. Check it out! (:
Tested in IE > 8
经过仔细审查,我建议将此作为此线程中更清晰的解决方案:
JSFiddle 中的演示
问题:
这是一点解释:
首先,让我们看一下当您将鼠标或 Tab 键移入字段时事件的顺序。
我们可以像这样记录所有相关事件:
某些浏览器尝试在
mouseup
或click
事件期间定位光标。这是有道理的,因为您可能希望在一个位置开始插入符号并拖动以突出显示某些文本。在您实际抬起鼠标之前,它无法指定插入符号的位置。因此,处理焦点的函数注定会过早响应,从而使浏览器覆盖您的定位。但问题是我们确实想处理焦点事件。它让我们知道有人第一次进入该领域。之后,我们不想继续覆盖用户选择行为。
解决方案:
相反,在
focus
事件处理程序中,我们可以快速附加click
(单击)和keyup
(制表符)的侦听器即将触发的事件。我们只想触发该事件一次。我们可以使用
.one("click keyup)
,但这会为每种事件类型调用一次事件处理程序相反,一旦按下 mouseup 或 keyup,我们要做的第一件事就是删除两者的处理程序,这样无论我们是按 Tab 键还是用鼠标键都无关紧要。该函数应该只执行一次。现在,我们可以在浏览器做出选择后调用
select()
,这样我们就可以确保覆盖默认行为。最后,为了提供额外的保护,我们可以将 事件命名空间 添加到
mouseup
和keyup
函数,因此.off()
方法不会删除可能正在播放的任何其他侦听器。在 IE 10+、FF 28+ 和 IE 10+ 中测试Chrome 35+
或者,如果您想使用 名为
once
的函数来扩展 jQuery,该函数将针对任何事件数量:那么你可以进一步简化代码,如下所示:
小提琴演示
After careful review, I propose this as a far cleaner solution within this thread:
Demo in jsFiddle
The Problem:
Here's a little bit of explanation:
First, let's take a look at the order of events when you mouse or tab into a field.
We can log all the relevant events like this:
Some browsers attempt to position the cursor during the
mouseup
orclick
events. This makes sense since you might want to start the caret in one position and drag over to highlight some text. It can't make a designation about the caret position until you have actually lifted the mouse. So functions that handlefocus
are fated to respond too early, leaving the browser to override your positioning.But the rub is that we really do want to handle the focus event. It lets us know the first time that someone has entered the field. After that point, we don't want to continue to override user selection behavior.
The Solution:
Instead, within the
focus
event handler, we can quickly attach listeners for theclick
(click in) andkeyup
(tab in) events that are about to fire.We only want to fire the event once. We could use
.one("click keyup)
, but this would call the event handler once for each event type. Instead, as soon as either mouseup or keyup is pressed we'll call our function. The first thing we'll do, is remove the handlers for both. That way it won't matter whether we tabbed or moused in. The function should execute exactly once.Now, we can call
select()
after the browser has made its selection so we're sure to override the default behavior.Finally, for extra protection, we can add event namespaces to the
mouseup
andkeyup
functions so the.off()
method doesn't remove any other listeners that might be in play.Tested in IE 10+, FF 28+, & Chrome 35+
Alternatively, if you want to extend jQuery with a function called
once
that will fire exactly once for any number of events:Then you can simplify the code further like this:
Demo in fiddle
这将完成工作并避免您无法再通过鼠标选择部分文本的问题。
This would do the work and avoid the issue that you can no longer select part of the text by mouse.
大多数这些解决方案的问题在于,当更改输入字段中的光标位置时,它们无法正常工作。
onmouseup
事件更改字段内的光标位置,该事件在onfocus
之后触发(至少在 Chrome 和 FF 中)。如果您无条件丢弃mouseup
,则用户无法使用鼠标更改光标位置。如果该字段当前没有焦点,该代码将有条件地阻止
mouseup
默认行为。它适用于以下情况:我已在 Chrome 31、FF 26 和 IE 11 中对此进行了测试。
The problem with most of these solutions is that they do not work correctly when changing the cursor position within the input field.
The
onmouseup
event changes the cursor position within the field, which is fired afteronfocus
(at least within Chrome and FF). If you unconditionally discard themouseup
then the user cannot change the cursor position with the mouse.The code will conditionally prevent the
mouseup
default behaviour if the field does not currently have focus. It works for these cases:I have tested this within Chrome 31, FF 26 and IE 11.
此版本适用于 ios,还修复了 Windows chrome 上的标准拖动选择
http://jsfiddle.net/Zx2sc /2/
This version works on ios and also fixes standard drag-to-select on windows chrome
http://jsfiddle.net/Zx2sc/2/
阅读此帖子找到了一个很棒的解决方案
Found a awesome solution reading this thread
我来自 2016 年底,这段代码仅适用于最新版本的 jquery(本例中为 jquery-2.1.3.js)。
I'm coming from late 2016 and this code just works in recent versions of jquery (jquery-2.1.3.js in this case).
我总是使用 requestAnimationFrame() 来跳过内部事件后机制,这在 Firefox 中完美运行。尚未在 Chrome 中进行测试。
I always use
requestAnimationFrame()
to jump over internal post-event mechanisms and this works perfectly in Firefox. Haven't tested in Chrome.或者您可以只使用
效果很好。
Or you can just use
<input onclick="select()">
Works perfect.如果您在两个输入之间快速切换,请使用clearTimeout以提高安全性。
clearTimeout 清除旧的超时...
Use clearTimeout for more security if you switch quickly between two input..
clearTimeout clean the old timeout...
您可以使用简单的代码:
You can use simple code:
与原生 JavaScript
select()
配合得很好。或者一般来说:
Works great with the native JavaScript
select()
.or in general:
我使用 FF 16.0.2 和 jquery 1.8.3,答案中的所有代码都不起作用。
我使用这样的代码并工作。
i using FF 16.0.2 and jquery 1.8.3, all the code in the answer didn't work.
I use code like this and work.