光标操作知多少?(input textarea 操作)

2024-04-20 00:58

本文主要是介绍光标操作知多少?(input textarea 操作),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文网址:http://blog.csdn.net/spy19881201/article/details/38360613

亲测IE情况下textarea不兼容,有时间回来改

本文带你领略input textarea 光标操作:

首先,对于ie和非ie要特殊处理,前端常识!

ie: range.moveStart("character", start);
        range.moveEnd('character', index);

非IE:el.setSelectionRange(start, end);

非IE的调用方式简单,不多解释;IE下的操作解释下,ie下有个叫textRange的东西,用来控制光标的相关操作的,可以参考这里(http://msdn.microsoft.com/en-us/library/ie/ms535872(v=vs.85).aspx)

在操作ie下的textarea时,参考了这篇美文 http://blog.csdn.net/qimiguang/article/details/10474189 赞!


效果图:



这里附上源码:

<!DOCTYPE html>
<html>
<head><meta http-equiv="content-type" content="text/html;charset=UTF-8"/><title>input 操作光标在最后</title>
</head>
<body>
<div style=" margin: auto;width: 800px"><div><input type="radio" name="radio" checked οnclick="radioChange(event);" value="1"><input id="name" type="text" value="my name is Willian smith" style="width: 200px"><input type="radio" name="radio" οnclick="radioChange(event);" value="2"><textarea id="content" style="width: 200px">my name is Willian smith</textarea></div><div><button οnclick="focusFirstFun();">光标在最前面</button><button οnclick="focusEndFun();">光标在最后面</button><button οnclick="focusIndexFun();">光标在第二个字符之后</button><button οnclick="focusPosFun();">光标位置</button><button οnclick="focusBeforeFun();">光标前移</button><button οnclick="focusNextFun();">光标后移</button></div><div><button οnclick="focus5Fun();">选中前五个字符</button></div><div><p>光标位置:<span id="cursorLabel"></span></p></div>
</div><script type="text/javascript">
var cursorLabel = document.getElementById('cursorLabel'),el = document.getElementById('name');//  el = document.getElementById('content');function radioChange(event) {var radio = event.target;if (radio.value == '1') {el = document.getElementById('name');} else if (radio.value == '2') {el = document.getElementById('content');}
}function focusFirstFun() {focusFirst(el);
}function focusEndFun() {focusEnd(el);
}function focusIndexFun() {focusIndex(el, 2)
}function focus5Fun() {focus(el, 0, 5);
}function focusPosFun() {cursorLabel.innerHTML = focusPos(el);
}function focusBeforeFun() {focusMove(el, -1);
}function focusNextFun() {focusMove(el, 1);
}/***************通用接口**********************/function focusFirst(el) {focus(el, 0, 0);
}function focusIndex(el, start) {focus(el, start);
}function focusEnd(el) {focus(el, el.value.length, el.value.length);
}function focus(el, start, index) {start = start === undefined ? 0 : start;index = index === undefined ? 0 : index;if (el.createTextRange) {//IE浏览器var range = el.createTextRange();range.collapse(true);range.moveStart("character", start);range.moveEnd('character', index);range.select();} else {//非IE浏览器var end = index === 0 ? start : index;el.setSelectionRange(start, end);el.focus();}
}function focusMove(el, index) {if (el.createTextRange) {//IE浏览器el.focus();var range = document.selection.createRange();range.collapse(false);var tempRange = document.selection.createRange();if (el.tagName.toLowerCase() == 'textarea') {var pos = getCursorPosition(el);if (pos.start <= 0) {index = 0;}} else { //inputtempRange.setEndPoint("StartToStart", el.createTextRange());if (tempRange.text.length < 0) {index = 0;}}range.move("character", index);range.select();} else {//非IE浏览器var newPos = el.selectionStart + index;var start = end = newPos <= 0 ? 0 : newPos;el.setSelectionRange(start, end);el.focus();}
}function focusPos(el) {var start = 0;if (el.createTextRange) {//IE浏览器el.focus();var range = document.selection.createRange();range.collapse(false);var tempRange = document.selection.createRange();if (el.tagName.toLowerCase() == 'textarea') {var pos = getCursorPosition(el);start = pos.start <= 0 ? 0 : pos.start;} else { //inputtempRange.setEndPoint("StartToStart", el.createTextRange());start = tempRange.text.length;}} else {//非IE浏览器start = el.selectionStart;}if (window.console) {console.log(start);}return start;
}/*** getCursorPosition Method** Created by Blank Zheng on 2010/11/12.* Copyright (c) 2010 PlanABC.net. All rights reserved.** The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.*/
function getCursorPosition(textarea) {var rangeData = {text: "", start: 0, end: 0 };textarea.focus();if (textarea.setSelectionRange) { // W3CrangeData.start = textarea.selectionStart;rangeData.end = textarea.selectionEnd;rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end) : "";} else if (document.selection) { // IEvar i,oS = document.selection.createRange(),// Don't: oR = textarea.createTextRange()oR = document.body.createTextRange();oR.moveToElementText(textarea);rangeData.text = oS.text;rangeData.bookmark = oS.getBookmark();// object.moveStart(sUnit [, iCount])// Return Value: Integer that returns the number of units moved.for (i = 0; oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart("character", -1) !== 0; i++) {// Why? You can alert(textarea.value.length)if (textarea.value.charAt(i) == '\n') {i++;}}rangeData.start = i;rangeData.end = rangeData.text.length + rangeData.start;}return rangeData;
}
/*** setCursorPosition Method** Created by Blank Zheng on 2010/11/12.* Copyright (c) 2010 PlanABC.net. All rights reserved.** The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license.*/
function setCursorPosition(textarea, rangeData) {if (!rangeData) {alert("You must get cursor position first.")}if (textarea.setSelectionRange) { // W3Ctextarea.focus();textarea.setSelectionRange(rangeData.start, rangeData.end);} else if (textarea.createTextRange) { // IEvar oR = textarea.createTextRange();// Fixbug :// In IE, if cursor position at the end of textarea, the setCursorPosition function don't workif (textarea.value.length === rangeData.start) {oR.collapse(false)oR.select();} else {oR.moveToBookmark(rangeData.bookmark);oR.select();}}
}
</script>
</body>
</html>


这篇关于光标操作知多少?(input textarea 操作)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918905

相关文章

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

Nginx概念、架构、配置与虚拟主机实战操作指南

《Nginx概念、架构、配置与虚拟主机实战操作指南》Nginx是一个高性能的HTTP服务器、反向代理服务器、负载均衡器和IMAP/POP3/SMTP代理服务器,它支持高并发连接,资源占用低,功能全面且... 目录Nginx 深度解析:概念、架构、配置与虚拟主机实战一、Nginx 的概念二、Nginx 的特点

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

使用Python在PDF中绘制多种图形的操作示例

《使用Python在PDF中绘制多种图形的操作示例》在进行PDF自动化处理时,人们往往首先想到的是文本生成、图片嵌入或表格绘制等常规需求,然而在许多实际业务场景中,能够在PDF中灵活绘制图形同样至关重... 目录1. 环境准备2. 创建 PDF 文档与页面3. 在 PDF 中绘制不同类型的图形python

Java 操作 MinIO详细步骤

《Java操作MinIO详细步骤》本文详细介绍了如何使用Java操作MinIO,涵盖了从环境准备、核心API详解到实战场景的全过程,文章从基础的桶和对象操作开始,到大文件分片上传、预签名URL生成... 目录Java 操作 MinIO 全指南:从 API 详解到实战场景引言:为什么选择 MinIO?一、环境

在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)

《在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)》DataGrip是JetBrains公司出品的一款现代化数据库管理工具,支持多种数据库系统,包括MySQL,:本文主要介绍在D... 目录前言一、登录 mysql 服务器1.1 打开 DataGrip 并添加数据源1.2 配置 MySQL

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

Python操作Excel的实用工具与库openpyxl/pandas的详细指南

《Python操作Excel的实用工具与库openpyxl/pandas的详细指南》在日常数据处理工作中,Excel是最常见的数据文件格式之一,本文将带你了解openpyxl和pandas的核心用法,... 目录一、openpyxl:原生 Excel 文件操作库1. 安装 openpyxl2. 创建 Exc

Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)

《Python实现Word文档自动化的操作大全(批量生成、模板填充与内容修改)》在职场中,Word文档是公认的好伙伴,但你有没有被它折磨过?批量生成合同、制作报告以及发放证书/通知等等,这些重复、低效... 目录重复性文档制作,手动填充模板,效率低下还易错1.python-docx入门:Word文档的“瑞士