如何检查光标是否位于指定的标记之间

How to check if cursor is between specified tags

本文关键字:于指定 之间 是否 光标 何检查 检查      更新时间:2023-09-26

textarea中有一些formatted文本,并且光标位于某个位置。我需要检查光标是否在某些特定标记之间。

示例:foo | bar

isBetween(['b', 'strong']); // should return true in above case

我有function,它在textarea中返回cursorposition(它有一些IE问题,但目前满足我)。所以,我只需要isBetween() function

谢谢你的帮助!

更新

<p>qwer<b>ty|ui</b>op</p>
isBetween(['p']); // should also return true, because the cursor in not only between 'b', it is between 'p' too

;您应该用span标记将标记之间的空格括起来。然后,在jQuery中,使用.mouseover()函数。

示例:

<b>TAG1</b>
<span id="spacer">&nbsp;</span>
<strong>TAG2</strong>
<script type="text/javascript">
    $("#spacer").mouseover(function(){
        //Do stuff
    });
</script>

跨度中必须有一些东西,所以在其中扔一个&nbsp;作为空间。

现场小提琴示例:http://jsfiddle.net/PrruT/

更新:

我不会为你做整个功能,但我告诉你,你可以这样设置:

/* I'm assuming you can figure out how to get the cursor position on your own, represented by var cursor */
function isBetween(selectorOne, selectorTwo){
    var left = $(selectorOne).position().left; //Left position of the object
    var right = $(selectorTwo).position().left + $(selectorTwo).width(); //Right XY position of the object
    if (cursor > left && cursor < right)
        return true;
    else
        return false;
}