1.获取光标位置 int index = editText.getSelectionStart(); 2.在光标处插入字符 int index = editText.getSelectionStart(); Editable editable = editText.getText(); editable.insert(index, "aaaa"); 3.删除光标前字符 int index = editText.getSelectionStart(); Editable editable
js获取光标位置 var TT = { /* * 获取光标位置 * @Method getCursorPosition * @param t element * @return number */ getCursorPosition:function(t){ if (document.selection) { t.focus(); var ds = document.selection; var range = ds.createRange(); var stored_range = ran
前言: 高亮显示输入框中的关键字符,这就必须得用到可编辑div(或其他标签)元素了,这时我们需要获取光标的位置,以便插入字符. 正文: 正常情况下获取光标位置,代码如下: function getPointPorsiton1() { if (window.getSelection) { var range = window.getSelection().getRangeAt(0);//创建range } else if (document.selection) { var range = doc
<html><head><title>TEST</title><style>body,td { font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;}</style><script type="text/javascript"> var start = 0; var end = 0; function add()
/** * 获取选中文字 * 返回selection,toString可拿到结果,selection含有起始光标位置信息等 **/ function getSelectText() { var text, userSelection = window.getSelection()||document.selection.createRange();// not IE || ie if (!(text = userSelection.text)) text = userSelection; ret