我有
<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/