你应该使用 pageX 或 pageY,像这样
$(document).ready ->
$(document).mousemove (e) ->
console.log("#{e.pageX}")
console.log("#{e.pageY}")
例如,如果您需要获取相对于 div 的位置
$(document).ready ->
$(document).mousemove (e) ->
console.log("#{e.pageX - $('#divID').offset().left}")
console.log("#{e.pageY - $('#divID').offset().top}")
应用于你的情况,它会给你这样的东西
$(document).ready ->
$('p').mousemove (e) ->
console.log("#{e.pageX - $('p').offset().left}")
console.log("#{e.pageY - $('p').offset().top}")
将鼠标移到包含您的文本的段落上会给您鼠标在该段落内的位置
看到它在这里工作
http://jsfiddle.net/zXnk9/
编辑
如果您需要获取悬停字符的索引,可以使用如下技巧:
将文本包装在与文本宽度完全相同的容器中
<span>The quick brown fox jumps over the lazy dog</span>
然后进行以下计算
$(document).ready ->
// define the container for the text you are intersted in
container = $('span')
// on mouseover
container.mouseover (e) ->
// get container width
container_width = container.width()
// compute the avg character width base on the container width and the number of characters contained in your text.
// (If you have some complex formatting inside your container, you would have to adjust this calculation.)
char_width = p_width / container.text().length
// get the position of your mouse inside
position_inside = e.pageX - container.offset().left
// define the index of the character you are interested in
char_index = Math.floor(position_inside / char_width) - 1
// use it as you wish
// print it for example
console.log("#{char_index}")
你可以在这里检查它的工作。我已将事件设置为单击,以便您可以在 fox 的 f 上精确尝试它,它返回 16。
http://jsfiddle.net/zXnk9/1/
编辑 2:以可靠的方式做你想做的事情
加载文档时,将容器内的每个字符放入一个 html 节点中,如下所示
$(document).ready ->
// set your container
container = $('span')
// define a replacement text string
replacement_container_text = ''
// iterate over each character inside your container
$(container.text().split('')).each (i, char) ->
// put it inside a span and add it to your replacement text string
replacement_container_text += '<span>' + char + '</span>'
// set the replacement text string as the html content of your container
// this replaces the original text with the initial text with each
// character wrapped into a span
// (which can then be caught as targets for your mousemove events)
container.html(replacement_container_text)
然后,您可以使用以下命令获取鼠标所在字符的索引
container.mousemove (e) ->
range_start = container.children('span').index $(e.target)
console.log(range_start)
这将适用于多行容器、段落等。
请参阅工作示例http://jsfiddle.net/2TBFV/