什么是香草JS或jQuery解决方案,将选择一个文本框的所有内容时,文本框接收焦点?
当前回答
<输入类型=“文本”
其他回答
$(document).ready(function() {
$("input:text").focus(function() { $(this).select(); } );
});
我的解决方案是:
var mouseUp;
$(document).ready(function() {
$(inputSelector).focus(function() {
this.select();
})
.mousedown(function () {
if ($(this).is(":focus")) {
mouseUp = true;
}
else {
mouseUp = false;
}
})
.mouseup(function () {
return mouseUp;
});
});
因此,鼠标通常会工作,但不会在通过输入获得焦点后取消选择
这不仅仅是Chrome/Safari的问题,我在Firefox 18.0.1中也遇到了类似的问题。有趣的是,这不会发生在MSIE!这里的问题是第一个鼠标点击事件强制取消选择输入内容,所以忽略第一个事件。
$(':text').focus(function(){
$(this).one('mouseup', function(event){
event.preventDefault();
}).select();
});
timeOut方法会导致一种奇怪的行为,并且阻塞每个鼠标升起事件,您无法删除再次单击输入元素的选择。
jQuery不是JavaScript,在某些情况下JavaScript更容易使用。
看看这个例子:
<textarea rows="10" cols="50" onclick="this.focus();this.select()">Text is here</textarea>
来源:CSS Tricks, MDN
HTML:
var textFiled = document.getElementById("text-filed"); 文本文件。addEventListener("focus", function() {this.select();}); 输入你的文本:<input type=" Text " id=" Text -filed" value="test with filed Text ">
使用JQuery:
$("#text-filed").focus(function() { $(this).select(); } );
使用React JS:
在各自的组成部分-
<input
type="text"
value="test"
onFocus={e => e.target.select()}
/>
推荐文章
- 在数组中获取所有选中的复选框
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;