是否可以在拖动时更改选择?

时间:2014-03-26 07:16:26

标签: javascript drag-and-drop

基本上,一旦“dragstart”事件被触发,我想重新配置选择以添加/删除用户试图移动的某些节点。有问题的拖拽的来源将是一个可信的div的子(ren)。这可能吗?

说明问题的小提琴(要查看问题,请尝试将“.Yay!”部分拖到输入框中): http://jsfiddle.net/3m6Rv/2/

相关代码:

$('div').on('dragstart', '*', function(e) {
    var sel = window.getSelection();
    var range = sel.getRangeAt(0);
    var textNode = range.commonAncestorContainer;

    //As an example, let's say I want to remove all non-alphanumeric characters from the start of the string
    var matches = textNode.data.match(/^[^\w]+/gi);
    if (matches.length && matches[0].length) {
        range.setStart(textNode, range.startOffset + matches[0].length);
    }
    //At this point, the selection I'm "dragging" should be shortened to remove whitespace/punctuation from the front - but it's not
});

0 个答案:

没有答案