开发者社区 问答 正文

js怎么获取光标的位置

js怎么获取光标的位置

展开
收起
云计算小粉 2018-05-10 19:59:39 2436 分享
分享
版权
举报
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 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问