700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 获得焦点时选择文本框的所有内容(Vanilla JS或jQuery)

获得焦点时选择文本框的所有内容(Vanilla JS或jQuery)

时间:2020-03-04 18:08:06

相关推荐

获得焦点时选择文本框的所有内容(Vanilla JS或jQuery)

本文翻译自:Select all contents of textbox when it receives focus (Vanilla JS or jQuery)

什么是Vanilla JS或jQuery解决方案,当文本框获得焦点时,它将选择文本框的所有内容?

#1楼

参考:/question/213n/获得焦点时选择文本框的所有内容-Vanilla-JS或jQuery

#2楼

$(document).ready(function() {$("input:text").focus(function () { $(this).select(); } ).mouseup(function (e) {e.preventDefault(); });});

#3楼

The answers here helped me up to a point, but I had a problem on HTML5 Number input fields when clicking the up/down buttons in Chrome.这里的答案帮助我达到了一定程度,但是当点击Chrome中的向上/向下按钮时,我在HTML5号码输入字段上遇到了问题。

If you click one of the buttons, and left the mouse over the button the number would keep changing as if you were holding the mouse button because the mouseup was being thrown away.如果单击其中一个按钮,并将鼠标悬停在按钮上,则数字会不断变化,就像您按住鼠标按钮一样,因为鼠标被丢弃了。

I solved this by removing the mouseup handler as soon as it had been triggered as below:我通过触发鼠标处理程序解决了这个问题,如下所示:

$("input:number").focus(function () {var $elem = $(this);$elem.select().mouseup(function (e) {e.preventDefault();$elem.unbind(e.type);});});

Hope this helps people in the future...希望这对未来的人们有所帮助......

#4楼

my solution is to use a timeout.我的解决方案是使用超时。Seems to work ok似乎工作正常

$('input[type=text]').focus(function() {var _this = this;setTimeout(function() {_this.select();}, 10);});

#5楼

This will work, Try this -这将有效,试试这个 -

<input id="textField1" onfocus="this.select()" onmouseup="return false" />

Works in Safari/IE 9 and Chrome, I did not get a chance to test in Firefox though.在Safari / IE 9和Chrome中工作,我没有机会在Firefox中测试。

#6楼

This is not just a Chrome/Safari issue, I experienced a quite similar behavior with Firefox 18.0.1.这不仅仅是Chrome / Safari问题,我在Firefox 18.0.1中遇到了类似的行为。The funny part is that this does not happen on MSIE!有趣的是,这不会发生在MSIE上!The problem here is the firstmouseupevent that forces to unselect the input content, so just ignore the first occurence.这里的问题是第一个强制取消选择输入内容的mouseup事件,所以只需忽略第一次出现。

$(':text').focus(function(){$(this).one('mouseup', function(event){event.preventDefault();}).select();});

The timeOut approach causes a strange behavior, and blocking every mouseup event you can not remove the selection clicking again on the input element.timeOut方法会导致奇怪的行为,并且阻止每个mouseup事件,您无法删除在输入元素上再次单击的选择。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。