开发者社区 问答 正文

js怎么获取光标的位置

js怎么获取光标的位置

展开
收起
云计算小粉 2018-05-10 19:59:39 2408 分享 版权
1 条回答
写回答
取消 提交回答
  • 努力在努力

    const getPosition = function (element) {

    let cursorPos = 0;
    if (document.selection) {//IE
        var selectRange = document.selection.createRange();
        selectRange.moveStart('character', -element.value.length);
        cursorPos = selectRange.text.length;
    } else if (element.selectionStart || element.selectionStart == '0') {
        cursorPos = element.selectionStart;
    }
    return cursorPos;

    }

    2019-07-17 22:10:11
    赞同 展开评论