最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect click but not drag using jQuery? - Stack Overflow

programmeradmin2浏览0评论

I have code that select text when user click the p tag but I don't want to do that when user select the text inside. Is it possible to detect click but without drag?

I have simple code like this:

$('.conteiner').on('click', 'p.xxx', function() {
   $(this).selection();
});

But click is executed even if I drag between mousedown and mouseup. The selection is a plugin that select text using getSelection or Range.

I have code that select text when user click the p tag but I don't want to do that when user select the text inside. Is it possible to detect click but without drag?

I have simple code like this:

$('.conteiner').on('click', 'p.xxx', function() {
   $(this).selection();
});

But click is executed even if I drag between mousedown and mouseup. The selection is a plugin that select text using getSelection or Range.

Share Improve this question edited Dec 16, 2020 at 8:36 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Feb 18, 2014 at 10:31 jcubicjcubic 66.5k58 gold badges248 silver badges449 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 16

Similar to this: Can you detect "dragging" in jQuery?

You can use mousedown and mouseup to detect whether there was a drag.

 $(function() {
    var isDragging = false;
    $(".conteiner")
    .mousedown(function() {
        $(window).mousemove(function() {
            isDragging = true;
            $(window).unbind("mousemove");
        });
    })
    .mouseup(function() {
        var wasDragging = isDragging;
        isDragging = false;
        $(window).unbind("mousemove");
        if (!wasDragging) {
            $(this).selection();
        }
    });
  });

Here is the plunker demo: http://embed.plnkr.co/Y7mrOP/

Found better way, since I need to detect drag to select text this works better:

var get_selected_text = (function() {
    if (window.getSelection || document.getSelection) {
        return function() {
            var selection = (window.getSelection || document.getSelection)();
            if (selection.text) {
                return selection.text;
            } else {
                return selection.toString();
            }
        };
    } else if (document.selection && document.selection.type !== "Control") {
        return function() {
            return document.selection.createRange().text;
        };
    }
    return function() {
        return '';
    };
})();

self.mouseup(function() {
    if (get_selected_text() === '') {
       // click not drag
    }
});

Type annotated version of the OP's solution for Closure Compiler in Advanced mode

const get_selected_text = (/** @return {function():string} */ function() {
    if (window.getSelection || document.getSelection) {
        return function () {
            const selection = /** @type {function():Object<string,?>} */ (window.getSelection || document.getSelection)();
            if (typeof selection['text'] === "string") {
                return selection['text'];
            } else {
                return selection.toString();
            }
        };
    } else if (document.selection && document.selection.type !== "Control") {
        return function () {
            return document.selection.createRange().text;
        };
    }
    return function () {
        return '';
    };
})();
发布评论

评论列表(0)

  1. 暂无评论