zoukankan      html  css  js  c++  java
  • js获取文本框(或文本域)光标位置以及设置光标位置

    var cursor = {
                /*获取文本框(或文本域)光标位置  ele:文本框(或文本域)*/
                getCursorPosition: function (element) {
                    var pos = 0;
                    if (document.selection) {/*IE*/
                        element.focus();
                        var range = document.selection.createRange();
                        range.moveStart("character", -form1.txt.value.length);
                        pos = range.text.length;
                    } else if (element.selectionStart) {
                        pos = element.selectionStart;
                    }
                    return pos;
                },
                /*设置文本框(或文本域)光标位置  ele:文本框(或文本域),pos位置(可选,默认为0)*/
                setCursorPosition: function (element, pos) {
                    pos = pos ? pos : 0;
                    if (element.createTextRange) {
                        var range = element.createTextRange();
                        range.collapse(true);
                        range.moveEnd('character', pos);
                        range.moveStart('character', pos);
                        range.select();
                    } else if (element.setSelectionRange) {
                        element.setSelectionRange(pos, pos);
                    }
                }
            }
  • 相关阅读:
    MySQL 慢日志没有自动创建新的日志文件
    Springboot为什么加载不上application.yml的配置文件
    android studio set proxy
    c++ win32 遍历进程列表
    React Prompt组件 阻止用户离开页面
    JS 浏览器上生成 UUID API
    部署 Nestjs 最佳实践
    Nginx 部署 单页面应用 + nodejs api 应用 最佳实践
    React JS: 如何使用 RxService 管理状态
    umijs 开发优化和生产优化
  • 原文地址:https://www.cnblogs.com/yuanxiaowa/p/3623589.html
Copyright © 2011-2022 走看看