分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

js 光标位置处理

发布时间:2023-09-06 01:17责任编辑:熊小新关键词: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(); ?????} ?}

js 光标位置处理

原文地址:http://www.cnblogs.com/xtreme/p/7660055.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved