zoukankan      html  css  js  c++  java
  • javascript中的光标

    最近项目中要做一个键盘操作,光标移动的功能;增强用户体验;问朋友查资料了解到这方面的知识;整理备忘;

    1、IE使用textRange对象,其他使用selectionStart selectionEnd;

    /**
     * [moveCursor description]
     * @param  {[type]} obj       [表单对象]
     * @param  {[type]} insertPos [插入位置]
     * @return {[type]}           [description]
     */
    function moveCursor(obj,insertPos){
        if(isNaN(insertPos)){
            throw '参数insertPos不是数字';
        }
        if(obj.nodeType != 1 ){
            throw '参数obj不是html对象'
        }
        insertPos = +insertPos;
        // document.body不支持新属性
        if('selectionStart' in obj){    
            obj.selectionStart = insertPos;
            obj.selectionEnd = insertPos;
            obj.focus();
        }else{
            var ctg = obj.createTextRange();
            ctg.move('character', insertPos);
            ctg.select();
        }
    }

    参考资料http://help.dottoro.com/ljtfkhio.php

  • 相关阅读:
    newman
    集合自动化
    56. Merge Intervals
    55. Jump Game
    48. Rotate Image
    34. Search for a Range
    33. Search in Rotated Sorted Array
    16. 3Sum Closest
    15. 3Sum
    11. Container With Most Water
  • 原文地址:https://www.cnblogs.com/w3cjiangtao/p/3353096.html
Copyright © 2011-2022 走看看