【问题标题】:How to Deselect selected input?如何取消选择选定的输入?
【发布时间】:2019-02-01 13:17:18
【问题描述】:

我有

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

并且拥有

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

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

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

你能提供如何取消选择输入的代码吗?但这不是focus,我说的不是选择输入本身,而是选择其中的文本。就像你双击文本一样。谢谢。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

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

此外,该字段的默认选择操作将被触发,因此将选择整个文本字段。

所以,如果你想要相反的结果,调用.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>

【讨论】:

    【解决方案2】:

    你可以使用:

    $('input').blur();
    

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 2014-04-23
      • 1970-01-01
      • 2011-08-17
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      相关资源
      最近更新 更多