文字选择活动结束? [英] End of text selection event?

查看:65
本文介绍了文字选择活动结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS上是否有结束文本选择的事件?

Is there an event for the end of text selection on iOS?

我知道当选择通过以下改变我可以运行一个事件:

I know I can run an event when the selection changes through the following:

document.addEventListener("selectionchange", function(event) {
        var text = window.getSelection().toString();
        $(".output").append("<div>" + text + "</div>");
}, false);

<div class="output"></div>

这将用选定的文本更新.output,但每次选择更改时都会运行.我想要的是仅在选择完成后捕获文本.

This will update .output with the selected text, but runs every time the selection changes. What I would want, is to only capture the text after the selection has finished.

有这样的事件吗?可以做这样的事情吗?

Is there any such event? Is it possible to do something like this?

推荐答案

与您类似,我没有找到解决此问题的好方法,因此我决定创建一种解决方法. 它不是最漂亮,但可以.

Similar to you i didn't found a good solution for this problem, So i decided to create a workaround. it's not the prettiest but it works.

我创建了一个超时函数,并将其绑定到"onselectionchange"事件. 每次触发该事件时,我都会检查我的超时是否正在运行,如果是,我将其删除并创建一个新的超时.

I created a timeout function and bind it to a "onselectionchange" event. Each time the event fired i check if my timeout is running, and if so i delete it and create a new one.

超时结束后,将触发自定义事件"selectionEnd".

when the timeout is finished a custom event "selectionEnd" is fired.

// bind selection change event to my function
document.onselectionchange = userSelectionChanged;

function userSelectionChanged() {

    // wait 500 ms after the last selection change event
    if (selectionEndTimeout) {
        clearTimeout(selectionEndTimeout);
    }

    selectionEndTimeout = setTimeout(function () {
        $(window).trigger('selectionEnd');
    }, 500);
}

$(window).bind('selectionEnd', function () {

    // reset selection timeout
    selectionEndTimeout = null;

    // TODO: Do your cool stuff here........

    // get user selection
    var selectedText = getSelectionText();

    // if the selection is not empty show it :)
    if(selectedText != ''){
       // TODO: Do something with the text
    }
});

演示: http://jsfiddle.net/dimshik/z8Jge/

我在博客中写了一篇关于它的小文章:

I wrote a small post about it in my blog: http://www.dimshik.com/end-of-text-selection-event-on-ios-workaround/

这篇关于文字选择活动结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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