【问题标题】:How to check if cursor is between specified tags如何检查光标是否在指定标签之间
【发布时间】:2012-09-03 14:21:28
【问题描述】:

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

示例: foo|bar.

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

我有function,它在cursor 中返回position textarea(有一些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

【问题讨论】:

    标签: javascript jquery internet-explorer tags cursor


    【解决方案1】:

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

    例子:

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

    跨度必须有一些东西,所以在里面放一个&amp;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;
    }
    

    【讨论】:

    • 另外,作为旁注,知道 已被弃用,您应该使用 css 来设置对象的样式 (&lt;span style='font-weight: bold;'&gt;Hello&lt;/span&gt;)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多