您现在的位置:首页 --> JavaScript --> 新浪操作textarea的工具函数
新浪操作textarea的工具函数
浏览:1888次 出处信息
从新浪的库上copy下来的操作textarea的工具函数,用做学习研究目的。
App.TextareaUtils = ( function () { var it = {}, ds = document.selection; it.selectionStart = function (oElement) { if (!ds) { return oElement.selectionStart } var er = ds.createRange(), value, len, s = 0; var er1 = document.body.createTextRange(); er1.moveToElementText(oElement); for (s; er1.compareEndPoints( "StartToStart" , er) < 0; s++) { er1.moveStart( "character" , 1) } return s }; it.selectionBefore = function (oElement) { return oElement.value.slice(0, it.selectionStart(oElement)) }; it.selectText = function (oElement, nStart, nEnd) { oElement.focus(); if (!ds) { oElement.setSelectionRange(nStart, nEnd); return } var c = oElement.createTextRange(); c.collapse(1); c.moveStart( "character" , nStart); c.moveEnd( "character" , nEnd - nStart); c.select() }; it.insertText = function (oElement, sInsertText, nStart, nLen) { oElement.focus(); nLen = nLen || 0; if (!ds) { var text = oElement.value, start = nStart - nLen, end = start + sInsertText.length; oElement.value = text.slice(0, start) + sInsertText + text.slice(nStart, text.length); it.selectText(oElement, end, end); return } var c = ds.createRange(); c.moveStart( "character" , -nLen); c.text = sInsertText }; it.getCursorPos = function (obj) { var CaretPos = 0; if ($IE) { obj.focus(); var range = null ; range = ds.createRange(); var stored_range = range.duplicate(); stored_range.moveToElementText(obj); stored_range.setEndPoint( "EndToEnd" , range); obj.selectionStart = stored_range.text.length - range.text.length; obj.selectionEnd = obj.selectionStart + range.text.length; CaretPos = obj.selectionStart } else { if (obj.selectionStart || obj.selectionStart == "0" ) { CaretPos = obj.selectionStart } } return CaretPos }; it.getSelectedText = function (obj) { var selectedText = "" ; var getSelection = function (e) { if (e.selectionStart != undefined && e.selectionEnd != undefined) { return e.value.substring(e.selectionStart, e.selectionEnd) } else { return "" } }; if (window.getSelection) { selectedText = getSelection(obj) } else { selectedText = ds.createRange().text } return selectedText }; it.setCursor = function (obj, pos, coverlen) { pos = pos == null ? obj.value.length : pos; coverlen = coverlen == null ? 0 : coverlen; obj.focus(); if (obj.createTextRange) { var range = obj.createTextRange(); range.move( "character" , pos); range.moveEnd( "character" , coverlen); range.select() } else { obj.setSelectionRange(pos, pos + coverlen) } }; it.unCoverInsertText = function (obj, str, pars) { pars = (pars == null ) ? {} : pars; pars.rcs = pars.rcs == null ? obj.value.length : pars.rcs * 1; pars.rccl = pars.rccl == null ? 0 : pars.rccl * 1; var text = obj.value, fstr = text.slice(0, pars.rcs), lstr = text.slice(pars.rcs + pars.rccl, text == "" ? 0 : text.length); obj.value = fstr + str + lstr; this .setCursor(obj, pars.rcs + (str == null ? 0 : str.length)) }; return it })(); |
建议继续学习:
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:memoize 实现代码中的小陷阱
后一篇:Nodejs和MongoDB初体验 >>
文章信息
- 作者:小寒 来源: 记事本
- 标签: textarea
- 发布时间:2011-01-18 22:17:25
近3天十大热文
- [70] Twitter/微博客的学习摘要
- [66] 如何拿下简短的域名
- [65] IOS安全–浅谈关于IOS加固的几种方法
- [64] find命令的一点注意事项
- [63] android 开发入门
- [63] Go Reflect 性能
- [61] 流程管理与用户研究
- [59] 图书馆的世界纪录
- [59] 读书笔记-壹百度:百度十年千倍的29条法则
- [59] Oracle MTS模式下 进程地址与会话信