如何检查光标是否在指定标签之间 [英] How to check if cursor is between specified tags

查看:121
本文介绍了如何检查光标是否在指定标签之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are some formatted text inside the textarea and a cursor is in some place. I need to check if the cursor is between some specific tags.

示例: foo | bar .

Example: foo|bar.

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

我有function可以返回textarea内部的cursorposition(存在一些IE问题,但是暂时让我满意).所以,我只需要isBetween() function.

I have function that returns position of the cursor inside textarea (is has some IE issues, but satisfies me for now). So, all I need is that 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() 函数.

 You should enclose the space between the tags with a span tag. Then, in jQuery, use the .mouseover() function.

示例:

<b>TAG1</b>
<span id="spacer">&nbsp;</span>
<strong>TAG2</strong>

<script type="text/javascript">
    $("#spacer").mouseover(function(){
        //Do stuff
    });
</script>

跨度必须有一些东西,因此在其中扔一个&nbsp;作为空格.

The span has to have something in it, so throw an &nbsp; in there as a space.

实时小提琴示例: http://jsfiddle.net/PrruT/

更新:

我不会为您完成全部功能,但我会告诉您可以这样设置:

I'm not going to do the whole function for you, but I'll tell you that you could set it up like this:

/* 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;
}

这篇关于如何检查光标是否在指定标签之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆