我的代码很简单:
<input id="thisInput" class="MyInput"
maxlength="3"
value=" 0"
type="text" />
和JS代码:
$("#thisInput").on( "click", function() {
$(this).select();
});
在Chrome中使用此代码。在第一次单击时选择输入中的所有值。另一个点击结束没有选择 在IE11中,每次单击结束时都会选择输入中的所有文本。
我的代码有点不对劲?或Chrome或IE被窃听? JSFIDDLe
答案 0 :(得分:4)
与@Jai commented一样,这是Chrome的行为。我个人认为这不是Chrome错误,也不是IE错误。
如果您想始终从输入中选择文本,可以使用异步调用来欺骗它:
$("#thisInput").on("click", function() {
setTimeout(function() {
$(this).select();
}.bind(this), 0);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="thisInput" class="MyInput" maxlength="3" value=" 0" type="text" />
&#13;
嗯,我看到的最佳工作解决方案是使用标志并切换它:
var select = false;
$("#thisInput").on("click", function() {
if (select = !select) {
$(this).select();
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="thisInput" class="MyInput" maxlength="3" value=" 0" type="text" />
&#13;
答案 1 :(得分:1)
可能会发生点击事件在IE中未正确触发,因此请尝试添加focus
事件
$("#thisInput").on( "click focus", function() {
$(this).select();
});
<强> JSFiddle Demo 强>