什么是香草JS或jQuery解决方案,将选择一个文本框的所有内容时,文本框接收焦点?
当前回答
如果你把这些事件链接在一起,我相信就不需要使用.one了,就像本文其他地方建议的那样。
例子:
$('input.your_element').focus( function () {
$(this).select().mouseup( function (e) {
e.preventDefault();
});
});
其他回答
这不仅仅是Chrome/Safari的问题,我在Firefox 18.0.1中也遇到了类似的问题。有趣的是,这不会发生在MSIE!这里的问题是第一个鼠标点击事件强制取消选择输入内容,所以忽略第一个事件。
$(':text').focus(function(){
$(this).one('mouseup', function(event){
event.preventDefault();
}).select();
});
timeOut方法会导致一种奇怪的行为,并且阻塞每个鼠标升起事件,您无法删除再次单击输入元素的选择。
我知道内联代码是不好的风格,但我不想把它放在.js文件中。 不用jQuery也能工作!
<input type="text" value="blah blah" onfocus="this.select(); this.selAll=1;" onmouseup="if(this.selAll==0) return true; this.selAll=0; return false;"></input>
我的解决方案是:
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;
});
});
因此,鼠标通常会工作,但不会在通过输入获得焦点后取消选择
这个会有用的,试试这个
<input id="textField1" onfocus="this.select()" onmouseup="return false" />
可以在Safari/IE 9和Chrome上运行,但我没有机会在Firefox上测试。
onclick="this.focus();this.select()"