当用户专注于该字段时,我正在使用此代码尝试选择字段中的所有文本.会发生什么,它选择全部一秒钟,然后取消选择,键入光标留在我点击的位置...
$("input[type=text]").focus(function() {
$(this).select();
});
Run Code Online (Sandbox Code Playgroud)
我希望这一切都保持选中状态.
什么是Vanilla JS或jQuery解决方案,当文本框获得焦点时,它将选择文本框的所有内容?
如何在iOS设备上以编程方式选择输入字段的文本,例如运行移动Safari的iPhone,iPad?
通常.select(),在<input ... />元素上调用函数就足够了,但这对这些设备不起作用.光标只是留在现有条目的末尾而没有做出选择.
我们的应用程序在输入字段上使用selectionStart来确定当用户按下箭头键时是否自动将用户移动到下一个/上一个字段(即,当选择位于文本末尾并且用户按下右箭头时我们移动到下一个字段,否则)
Chrome现在阻止在type ="number"的地方使用selectionStart.它现在抛出异常:
Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection.
Run Code Online (Sandbox Code Playgroud)
见如下:
https://codereview.chromium.org/100433008/#ps60001
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#do-not-apply
有没有办法在type ="number"的输入字段中确定插入符的位置?
我在这个问题中遇到与用户相同的问题,这是由于Webkit中的这个错误.但是,提供的解决方法不适用于我的应用程序.让我重新陈述问题,这样你就不必去读另一个问题了:
我正在尝试选择textarea中的所有文本,当它获得焦点时.以下jQuery代码适用于IE/FF/Opera:
$('#out').focus(function(){
$('#out').select();
});
Run Code Online (Sandbox Code Playgroud)
但是,在Chrome/Safari中,文本被选中 - 非常简短 - 但随后会触发mouseUp事件并取消选择文本.以上链接提供了以下解决方法:
$('#out').mouseup(function(e){
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
但是,这种解决方法对我没有好处.我想只在用户给出textarea焦点时选择所有文本.然后,如果他选择,他必须能够只选择部分文本.任何人都可以想到仍然符合此要求的解决方法吗?
我想创建一个输入框,当用户选择它时,框中的所有文本都被选中.所以我决定使用这个select()功能,这就是我想要实现的功能.但是,它可以像我预期的那样在桌面浏览器上运行,但不能在iPad上的Safari上运行.如何解决问题?谢谢.
$('#input').click(function(e){
$(this).select();
});
Run Code Online (Sandbox Code Playgroud) 我们正在为移动浏览器开发Web-App,我们希望只要输入框获得焦点,就可以选择任何输入框的文本.以下答案修复了Desktop chrome/safari浏览器的问题.以下解决方案适用于Android浏览器,但不适用于iPhone/iPad浏览器,任何解决方案..
$("#souper_fancy").focus(function() { $(this).select() });
$("#souper_fancy").mouseup(function(e){
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
参考: -
焦点事件在单击Safari时在所有浏览器(Safari)上均能正常运行。
而且它在div [tabindex]元素上也可以正常工作,但不适用于button或input:button元素。
当我使用$('。btn')。focus()时,它也可以获取焦点事件。
为什么焦点事件没有在点击时触发?
这是我的代码:
$(".btn").focus(function (e) {
$(".result").append("<p>Event:"+e.type+", target:"+e.target+"</p>");
})Run Code Online (Sandbox Code Playgroud)
.result{min-height:200px;border:1px solid #000;}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="btn">button 1</button>
<input type="button" class="btn" value="button 2"/>
<div class="btn" tabindex="0">div element</div>
<h1>
Result:
</h1>
<div class="result">
</div>Run Code Online (Sandbox Code Playgroud)
我在表格单元格中有一堆文本输入,如下所示:
<td class="tdTextInput">
<input type="text" value="0" name="txt1_9_4_2" id="txt1_9_4_2" class="input-supermini">
</td>
Run Code Online (Sandbox Code Playgroud)
每当用户点击单元格或输入时,它必须自动选择输入内的所有内容(有点像电子表格编辑器).
所以这里的脚本到目前为止只能在可靠的旧Firefox中成功实现.
//focus the textbox on td click
$('.tdTextInput').mousedown(function ()
{
$(this).find('input').first().focus();
});
//select all text on focus
$('.tdTextInput input').focus(function ()
{
//the data-selected attribute is used to prevent the
// autoselection to happen more than once per cell so that
// two consecutive clicks will allow the user to pinpoint the
// cursor to a specific position
var isSelected = $(this).attr('data-selected') == 'true';
if (!isSelected) {
$('input[data-selected]').removeAttr('data-selected');
$(this).attr('data-selected', 'true'); …Run Code Online (Sandbox Code Playgroud) 试图弄清楚为什么会发生这种情况 - 我有一个输入文本字段,我想要在字段获得焦点时突出显示所有文本.这很快就会发生,然后所有文本都被取消选中.知道为什么会这样吗?这是我正在使用的代码:
$("#permalink").focus(function(){
this.select();
});
Run Code Online (Sandbox Code Playgroud)