如何在javascript中使输入字段可编辑.我的意思是onFocus将它置于插入模式,以便可以覆盖值.有什么建议 ???
这应该适用于现代浏览器(也适用于移动设备):
var input = document.querySelector('input'); // or a textarea
input.addEventListener('keypress', function(){
var s = this.selectionStart;
this.value = this.value.substr(0, s) + this.value.substr(s + 1);
this.selectionEnd = s;
}, false);
Run Code Online (Sandbox Code Playgroud)
注意:这是插入功能的基本形式,因此某些默认功能(如CTRL + Z)可能会中断.
做了一些谷歌搜索,这似乎是相关的.它可能正在使用以下代码尝试播放,但它可能只适用于特定操作系统上的特定浏览器,但无论如何它都值得一试.
document.execCommand('OverWrite', false, true);
document.execCommand('OverWrite', false, false);
Run Code Online (Sandbox Code Playgroud)
根据您的要求,我会说实现会像这样:
<input type="text"
onFocus="document.execCommand('OverWrite', false, true);"
onBlur="document.execCommand('OverWrite', false, false);">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9735 次 |
| 最近记录: |