zoukankan      html  css  js  c++  java
  • js 光标位置处理

      /**
    * 获取选中文字
    * 返回selection,toString可拿到结果,selection含有起始光标位置信息等
    **/
    function getSelectText() { var text, userSelection = window.getSelection()||document.selection.createRange();// not IE || ie if (!(text = userSelection.text)) text = userSelection; return text; } /**
    * 获取光标位置
    * 对 input、textarea有效,对contenteditable:true方式不生效
    **/
    function getCursortPosition (textDom) { var cursorPos = 0; if (document.selection) { // IE Support textDom.focus (); var selectRange = document.selection.createRange(); selectRange.moveStart ('character', -textDom.value.length); cursorPos = selectRange.text.length; }else if (textDom.selectionStart || textDom.selectionStart == '0') { // 非 IE cursorPos = textDom.selectionStart; } return cursorPos; } /**
    * 设置光标位置
    * 有效对象同光标获取
    **/
    function setCaretPosition(textDom, pos){ if(textDom.setSelectionRange) { // IE Support textDom.focus(); textDom.setSelectionRange(pos, pos); }else if (textDom.createTextRange) { // 非 IE var range = textDom.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } }
  • 相关阅读:
    并不对劲的辛普森积分
    并不对劲的概率与期望
    并不对劲的cdq分治解三维偏序
    68.机器人的运动范围
    67.矩阵中的路径
    66.滑动窗口最大值
    65.数据流的中位数
    64.二叉搜索树的第K个节点
    63.序列化二叉树
    62.把二叉树打印成多行
  • 原文地址:https://www.cnblogs.com/xtreme/p/7660055.html
Copyright © 2011-2022 走看看