如何在点击的位置插入字符 文本框里现在有一些字,现在打算点其中间利用JS加一个字符在网上看到有一种解决方案是利用选中的方式模拟,用selection.createRange();方式但是这样方式 实际上只用IE是可以的 IE貌似把点击看成选中了一小点但是谷歌,火狐等浏览器是不兼容的谁有完成 点击后 插入字符的兼容JS代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script> function AddText(txt) { obj = document.getElementById('message'); selection = document.selection; checkFocus(); if(typeof(obj.selectionStart)!='undefined') { //document.getElementById('logs').innerHTML+=obj.selectionStart+' '+obj.selectionEnd; obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd); } else if(selection && selection.createRange) { var sel = selection.createRange(); sel.text = txt; sel.moveStart('character', -txt.length); } else { obj.value += txt; } } function checkFocus() { obj.focus(); }</script><textarea id="message" name="message" onClick="AddText('test')">12345</textarea>IE,FF下都测试了。 关于dojo.data中dojo.fetch不能正常运行的问题 如何获得当前IE高度? js画矩形框代码 js的replace替换问题! 这个对象如何传递呀 关于正则表达式,如何设定搜索深度? 我的原代码,看能不能实现这个功能?在线~~ 弹出窗口置顶层,有办法吗? redio如何调制text框是否为disabled 那里有有关javascript在netscape6下使用的资料?急!多谢! xmlDoc创建xml文档 编码格式?、xmlhttp传输xml数据、服务器保存xml文件、中文乱码啦 奇怪‘6 - + 1’用eval 也能算出结果,何解....
<script>
function AddText(txt) {
obj = document.getElementById('message');
selection = document.selection;
checkFocus();
if(typeof(obj.selectionStart)!='undefined') {
//document.getElementById('logs').innerHTML+=obj.selectionStart+' '+obj.selectionEnd;
obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
} else if(selection && selection.createRange) {
var sel = selection.createRange();
sel.text = txt;
sel.moveStart('character', -txt.length);
} else {
obj.value += txt;
}
}
function checkFocus() {
obj.focus();
}
</script>
<textarea id="message" name="message" onClick="AddText('test')">12345</textarea>IE,FF下都测试了。