javascript - 如何取消选择选定的输入?

标签 javascript jquery

我有

<input type="text" id="first"> 

 $('#first').select();

所以,在某些情况下我想取消选择输入,比如

$('#first').deselect();

您能否提供如何取消选择所选输入的代码?但这不是焦点,我说的不是选择input本身,而是选择里面的文字。就像您双击文本一样。谢谢。

最佳答案

基于 jQuerys API 文档,.select() is simply a helper function that performs focusing and selection of the text node :

In addition, the default select action on the field will be fired, so the entire text field will be selected.

因此,如果您想要相反的结果,调用 .blur() 将产生相反的效果。它将从场散焦并重置选择。请参阅下面的概念验证:

$('#select').on('click', function() {
  $('#first').select();
});

$('#deselect').on('click', function() {
  $('#first').blur();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="first" value="Lorem ipsum">
<button type="button" id="select">Select</button>
<button type="button" id="deselect">Deselect</button>

关于javascript - 如何取消选择选定的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480350/

相关文章:

javascript - 使用 appendchild 属性在 div 中附加元素

javascript、循环引用和内存泄漏

javascript - 如何查找 src 为空的 Img 标签。并删除它

jquery - SlideUp() 导致元素消失(在 IE10 中)

Jquery 切换有时不会出现 100%

javascript - Bootstrap标签输入

javascript - jQuery .data(),需要接受数组元素作为键或找到替代方案(JSON?)

JQuery UI 日期选择器 minDate 和日期范围看起来很糟糕

javascript - undefined variable 的clearInterval问题原因

javascript - 需要在 jQuery 中获取元素的文本值