jquery - 用于 jQuery 文本编辑器样式选择的插件?

标签 jquery jquery-ui select text-editor jquery-ui-selectable

我想要实现的是使用 jQuery 来模仿您在典型文本编辑器中看到的文本选择功能的行为,除了选择文本之外,我想选择多行 <div> s。然而,到目前为止,我发现的 jQuery 唯一“选择”插件是基于矩形套索模型运行的。特别是,我正在使用 jQueryUI 可选择插件。要了解我在说什么,请考虑以下两张图片:

默认 jQueryUI“可选择”插件行为

理想的插件行为(无套索) http://img709.imageshack.us/img709/5664/selectableidealthumb.png

您也可以去here来玩一下这个确切的例子。有人知道有一个插件可以实现这个功能吗?这将使我免于继续破解或破解这个插件以获得我想要的东西......

P/S:在我的应用程序中,每行最多包含 150 个左右的 div,每个 div 中都会包含一些 div。我尝试过手动滚动我自己的选择,但即使只处理一行,它也很慢。我目前正在使用这个插件,因为它比我编写的性能要好得多。

最佳答案

也许这可以以某种方式进行优化,但我只在 Chrome 中测试过它,但我认为它也适用于其他浏览器。为此不需要 jQuery UI,它是手工制作的;)

$(function() {
    var selectableLi = $('#selectable li');
    selectableLi.mousedown(function(){
        var startIndex, endIndex, mouseUpOnLi = false;

        // When dragging starts, remove classes active and hover
        selectableLi.removeClass('active hover');

        // Give the element where dragging starts a class
        $(this).addClass('active');

        // Save the start index
        startIndex = $(this).index();

        // Bind mouse up event 
        selectableLi.bind('mouseup', function(){

            // Mouse up is on a li-element
            mouseUpOnLi = true;
            $(this).addClass('active');

            // Remove the events for mouseup, mouseover and mouseout
            selectableLi.unbind('mouseup mouseover mouseout');

            // Store the end index
            endIndex = $(this).index();

            // Swap values if endIndex < startindex
            if(endIndex < startIndex){
                var tmp = startIndex;
                startIndex = endIndex;
                endIndex = tmp;                 
            }

            // Give the selected elements a colour
            for(i=startIndex; i<=endIndex; i++){
                $(selectableLi[i]).addClass('active');
            }

        }).bind('mouseover', function(){
            // Give elements a hover class when hovering
            $(this).addClass('hover');
        }).bind('mouseout', function(){
            // Remove the hover class when mouse moves out the li
            $(this).removeClass('hover');
        });

        $(document).bind('mouseup', function(e){
            // When mouse up is outside a li-element
            if(!mouseUpOnLi){
                selectableLi.removeClass('active');
            }
            $(this).unbind('mouseup');
        });
    }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false});
});

我有一个 example online 。请注意,选择时项目没有背景颜色;我认为这会带来更好的性能。

<小时/>

更新 - Example 2

我更新了它,以便在选择时可以看到选择:

var selectableLi;

function colourSelected(a, b, Class){
    selectableLi.removeClass(Class);
    // Swap values if a > b
    if(a > b){
        var tmp = a;
        a = b;
        b = tmp;                    
    }

    // Give the selected elements a colour
    for(i=a; i<=b; i++){
        $(selectableLi[i]).addClass(Class);
    }       
}

$(function() {
    selectableLi = $('#selectable li');
    selectableLi.mousedown(function(){
        var startIndex, endIndex, mouseUpOnLi = false;

        // When dragging starts, remove classes active and hover
        selectableLi.removeClass('active hover');

        // Give the element where dragging starts a class
        $(this).addClass('active');

        // Save the start index
        startIndex = $(this).index();

        // Bind mouse up event 
        selectableLi.bind('mouseup', function(){

            // Mouse up is on a li-element
            mouseUpOnLi = true;
            $(this).addClass('active');

            // Remove the events for mouseup, mouseover and mouseout
            selectableLi.unbind('mouseup mouseover mouseout');

            colourSelected(startIndex, $(this).index(), 'active');

        }).bind('mouseover mouseout', function(){
            // Give elements a hover class when hovering
            colourSelected(startIndex, $(this).index(), 'hover');
        });

        $(document).bind('mouseup', function(e){
            // When mouse up is outside a li-element
            if(!mouseUpOnLi){
                selectableLi.removeClass('active hover');
            }
            $(this).unbind('mouseup');
            selectableLi.unbind('mouseover mouseout');
        });
    }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false});
});

同样,也许可以以某种方式优化此代码以提高性能。

关于jquery - 用于 jQuery 文本编辑器样式选择的插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2344449/

相关文章:

JQuery UI 可排序在 IE8 中很慢,但在 IE7 和 IE8 兼容模式下运行良好

jquery - 从随机定义的属性中获取值是否可取?

jQuery UI 选项卡转到选项卡中的特定 div

javascript - 有没有办法显示浏览器不支持的字体?

jquery - 如何将jquery日期选择器限制为仅当前月份?

android - Android 的非阻塞 IO

mysql - 一次 `Select`查询生成12个结果集

mysql - 从mysql表中获取所有日期数据的计数总和

javascript - 如何在 jQuery 中使用数据属性?

php - 检测 AdBlocking 软件?