代码之家  ›  专栏  ›  技术社区  ›  KoboldMines

如何取消选定输入?

  •  2
  • KoboldMines  · 技术社区  · 7 年前

    我有

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

    并拥有

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

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

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

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

    2 回复  |  直到 7 年前
        1
  •  4
  •   Terry    7 年前

    基于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
  •  1
  •   Marcos Pinho    7 年前

    你可以使用:

    $('input').blur();