How to check if cursor is between specified tags

*爱你&永不变心* 提交于 2020-01-05 07:00:05

问题


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.

Example: foo|bar.

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

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.

Thanks for any help!

Update:

<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

回答1:


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

Example:

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

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

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

Live fiddle example: http://jsfiddle.net/PrruT/

UPDATE:

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


来源:https://stackoverflow.com/questions/12249673/how-to-check-if-cursor-is-between-specified-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!