JavaScript的集中删除文本高亮显示 [英] Javascript focus remove text highlight

查看:127
本文介绍了JavaScript的集中删除文本高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有更新面板输入(文本字段),它的文字每次更改后autopostbacks。我可以保持聚焦在同一个文本字段,但我无法摆脱它调用的document.getElementById('myTextField将')之后出现的文本higlight的。重点()。
这种解决方案似乎是最准确的

I have an input (text field) in update panel, and it autopostbacks after each change of text. I can keep focus on the same text field, but i can't get rid of text higlight which appears after calling document.getElementById('myTextField').focus(). This solution seemed to be the most accurate:

if (window.getSelection) {
    window.getSelection().removeAllRanges();
} else if (document.selection) {
    document.selection.empty();
}

但它有一个问题。输入保持专注,但我不能写的文字。我写之前,点击它。

But it has one problem. Input remains focused, but i can't write text. I have to click on it before writing.

推荐答案

您可以做到这一点,如果你重新聚焦后你的价值,即

You can do it if you reset your value after focus, i.e.

HTML

<input id="myTextField" type="text" value="SomeValue" />​

JS

var myInput=document.getElementById('myTextField');
var myInput_value=myInput.value;
myInput.focus();
myInput.value=myInput_value;​

工作示例。

这篇关于JavaScript的集中删除文本高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆