js 如何获取光标的坐标(是在文体框中的坐标而非鼠标的光标)

jiangpan 2009-04-02 04:40:45
我主要想实现在网页的文体框中录入文字时有光标的位置上显示一个DIV的提示信息。效果就如同输入法一样。
...全文
672 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2009-04-02
  • 打赏
  • 举报
回复
from
http://weblogs.asp.net/skillet/archive/2005/03/24/395838.aspx
<html>

<head>

<script language="JavaScript" type="text/javascript">

<!--

function initPosition(textBox) {

var storedValue = textBox.value;

textBox.value = "";

textBox.select();



var caretPos = document.selection.createRange();

textBox.__boundingTop = caretPos.boundingTop;

textBox.__boundingLeft = caretPos.boundingLeft;



textBox.value = " ";

textBox.select();



caretPos = document.selection.createRange();

textBox.__boundingWidth = caretPos.boundingWidth;

textBox.__boundingHeight = caretPos.boundingHeight;



textBox.value = storedValue;

}



function storePosition(textBox) {

var caretPos = document.selection.createRange();



var boundingTop = (caretPos.offsetTop + textBox.scrollTop) - textBox.__boundingTop;

var boundingLeft = (caretPos.offsetLeft + textBox.scrollLeft) - textBox.__boundingLeft;



textBox.__Line = (boundingTop / textBox.__boundingHeight) + 1;

textBox.__Column = (boundingLeft / textBox.__boundingWidth) + 1;

}



function updatePosition(textBox) {

storePosition(textBox);

document.forms[0].txtLine.value = textBox.__Line;

document.forms[0].txtColumn.value = textBox.__Column;

}

//-->

</script>

<style type="text/css">

body, td, tg, input, select {

font-family: Verdana;

font-size: 10px;

}

</style>

</head>

<body onload="initPosition(document.forms[0].txtLayoutViewer)">

<form>

<table cellspacing="0" cellpadding="3">

<tr>

<td colspan="3">

Change Font Size

<select onchange="this.form.txtLayoutViewer.style.fontSize = this.options[this.selectedIndex].value; initPosition(this.form.txtLayoutViewer);">

<option value="10">10px</option>

<option value="12">12px</option>

<option value="14">14px</option>

<option value="16">16px</option>

<option value="18">18px</option>

<option value="20">20px</option>

<option value="24">24px</option>

<option value="36">36px</option>

</select>

</td>

</tr>

<tr>

<td colspan="3">

<textarea name="txtLayoutViewer"

onmouseup="updatePosition(this)"

onmousedown="updatePosition(this)"

onkeyup="updatePosition(this)"

onkeydown="updatePosition(this)"

onfocus="updatePosition(this)"

rows="15"

cols="75">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec ornare aliquam quam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Pellentesque et quam in dui consequat tempor. Etiam lorem lectus, sollicitudin laoreet, tincidunt nec, pharetra in, magna. Mauris accumsan velit et augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</textarea></td>

</tr>

<tr>

<td width="70%">

 </td>

<td width="10%">

Line <input type="text" name="txtLine" style="width: 25px" readonly></td>

<td width="20%">

Column <input type="text" name="txtColumn" style="width: 25px" readonly></td>

</tr>

</table>

</form>

</body>

</html>
 【为什么购买本课程?】1、学会Photoshop脚本,可以大幅提高您的工作效率、摆脱大量重复设计任务的束缚;2、扩展Photoshop的功能:天气预报、to-do设计任务管理、中文加拼音、图层文字中英互译、每日一句英语、OCR智能识别图片上的文字内容、 为上万影片批量生成九宫格预览图、为数百个视频自动添加内容不同的片头、自动获取图片的主题颜色、快速生成不限数量并且不重复的漂亮卡通头像、给Photoshop添加猜数字、贪吃蛇游戏等等;3、利用人工智能技术:在Photoshop中识别图片中指定颜色的物体、进行面部识别;4、掌握99%Photoshop设计师不曾接触的技能,打造自己的职场护城河!5、重要的是:只有我们这里提供系统、全面、易学的Photoshop脚本教程,只此一家,别无选择!  【Photoshop脚本是什么?】* Photoshop神秘和强大的一项秘技!* 它可以允许您以代码的方式来操作文档、美化图像、处理图层、控制通道、编辑选区、使用滤镜等等,就像在Photoshop界面上操作一样。* 与PhotoShop动作(Action)相比,PhotoShop脚本更强大、更智能、更富有逻辑判断功能。* PhotoShop脚本主要用于重复性的任务或用于制作非常复杂的特殊效果。 【学会Photoshop脚本,可以做什么?】* 一键给n个图片批量添加水印;* 一键将n个图片批量生成指定尺寸的缩略图;* 一键将PSD网页设计稿的各功能区域,批量输出为Web所用格式;* 一键生成iOS、Andriod应用和游戏必需的十几种尺寸的图标;* 一键将n个小图拼合为一张大图,并输出各小图在大图中的坐标信息;* 甚至开发一款运行在Photoshop上的趣味游戏!我们向您保证,学习PhotoShop脚本所花费的时间,可以在完成几项重复性的复杂任务时得到补偿。快来学习Photoshop脚本吧!  【课程的特点】1、创新的教学模式:手把手教您Photoshop自动化技术,一看就懂,一学就会;2、贴心的操作提示:让您的眼睛始终处于操作的焦点位置,不用再满屏找光标;3、语言简洁精练:瞄准问题的核心所在,减少对思维的干扰,并节省您宝贵的时间;4、视频短小精悍:即方便于您的学习和记忆,也方便日后对功能的检索;5、课程源码素材:购买课程之后,进入最后一章的最后一节的课件列表,下载课程源码素材。 

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧