【问题标题】:Get the pasted content on document on paste event在粘贴事件中获取文档上的粘贴内容
【发布时间】:2012-01-19 23:40:48
【问题描述】:

正如问题中提到的,我如何才能在文档上获取粘贴的内容。截至目前,我正在创建一个文本区域并使用 dblclick 将焦点放在 textarea 中,然后在 textarea 的粘贴事件上我正在收集数据。我不认为这是一个好方法。我的代码在下面

$('body').dblclick(function()
{
      $('#textare').focus();
});

Then 
$('#textare').keyup(function()
{
      alert( $(this).val() );
});

请建议我一个替代过程。
我正在寻找像

这样的替代方案
$(document).paste(function()
{
    // Get the pasted content
});

我正在使用谷歌浏览器。我不想使用 textarea 来捕捉文本。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您可以将onpaste 事件添加到您的元素。所有浏览器都支持。

 onpaste="return getPastedValue(this);"


 <script type="text/javascript">
        function getPastedValue (obj) {
            alert(obj.innerHTML);
           return false;
      }
 </script>

【讨论】:

  • 感谢您的回答,但我已经明确提到我想使用替代方法来替代当前方法。
【解决方案2】:

此解决方案比仅获取文本更进一步,它实际上允许您在将粘贴的内容粘贴到元素中之前对其进行编辑。

它通过使用 contenteditable、onpaste 事件(所有主流浏览器都支持)和突变观察者(Chrome、Firefox 和 IE11+ 支持)来工作

第一步

使用 contenteditable 创建一个 HTML 元素

<div contenteditable="true" id="target_paste_element"></div>

第 2 步

在您的 Javascript 代码中添加以下事件

document.getElementById("target_paste_element").addEventListener("paste", pasteEventVerifierEditor.bind(window, pasteCallBack), false);

我们需要绑定 pasteCallBack,因为突变观察者会被异步调用。

第 3 步

将以下函数添加到您的代码中

function pasteEventVerifierEditor(callback, e)
{
   //is fired on a paste event. 
    //pastes content into another contenteditable div, mutation observer observes this, content get pasted, dom tree is copied and can be referenced through call back.
    //create temp div
    //save the caret position.
    savedCaret = saveSelection(document.getElementById("target_paste_element"));

    var tempDiv = document.createElement("div");
    tempDiv.id = "id_tempDiv_paste_editor";
    //tempDiv.style.display = "none";
    document.body.appendChild(tempDiv);
    tempDiv.contentEditable = "true";

    tempDiv.focus();

    //we have to wait for the change to occur.
    //attach a mutation observer
    if (window['MutationObserver'])
    {
        //this is new functionality
        //observer is present in firefox/chrome and IE11
        // select the target node
        // create an observer instance
        tempDiv.observer = new MutationObserver(pasteMutationObserver.bind(window, callback));
        // configuration of the observer:
        var config = { attributes: false, childList: true, characterData: true, subtree: true };
         
        // pass in the target node, as well as the observer options
        tempDiv.observer.observe(tempDiv, config);

    }   

}



function pasteMutationObserver(callback)
{

    document.getElementById("id_tempDiv_paste_editor").observer.disconnect();
    delete document.getElementById("id_tempDiv_paste_editor").observer;

    if (callback)
    {
        //return the copied dom tree to the supplied callback.
        //copy to avoid closures.
        callback.apply(document.getElementById("id_tempDiv_paste_editor").cloneNode(true));
    }
    document.body.removeChild(document.getElementById("id_tempDiv_paste_editor"));

}

function pasteCallBack()
{
    //paste the content into the element.
    restoreSelection(document.getElementById("target_paste_element"), savedCaret);
    delete savedCaret;
    
    pasteHtmlAtCaret(this.innerHTML, false, true);
}   


saveSelection = function(containerEl) {
if (containerEl == document.activeElement)
{
    var range = window.getSelection().getRangeAt(0);
    var preSelectionRange = range.cloneRange();
    preSelectionRange.selectNodeContents(containerEl);
    preSelectionRange.setEnd(range.startContainer, range.startOffset);
    var start = preSelectionRange.toString().length;

    return {
        start: start,
        end: start + range.toString().length
    };
}
};

restoreSelection = function(containerEl, savedSel) {
    containerEl.focus();
    var charIndex = 0, range = document.createRange();
    range.setStart(containerEl, 0);
    range.collapse(true);
    var nodeStack = [containerEl], node, foundStart = false, stop = false;

    while (!stop && (node = nodeStack.pop())) {
        if (node.nodeType == 3) {
            var nextCharIndex = charIndex + node.length;
            if (!foundStart && savedSel.start >= charIndex && savedSel.start <= nextCharIndex) {
                range.setStart(node, savedSel.start - charIndex);
                foundStart = true;
            }
            if (foundStart && savedSel.end >= charIndex && savedSel.end <= nextCharIndex) {
                range.setEnd(node, savedSel.end - charIndex);
                stop = true;
            }
            charIndex = nextCharIndex;
        } else {
            var i = node.childNodes.length;
            while (i--) {
                nodeStack.push(node.childNodes[i]);
            }
        }
    }

    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}

function pasteHtmlAtCaret(html, returnInNode, selectPastedContent) {
//function written by Tim Down

var sel, range;
if (window.getSelection) {
    // IE9 and non-IE
    sel = window.getSelection();
    if (sel.getRangeAt && sel.rangeCount) {
        range = sel.getRangeAt(0);
        range.deleteContents();

        // Range.createContextualFragment() would be useful here but is
        // only relatively recently standardized and is not supported in
        // some browsers (IE9, for one)
        var el = document.createElement("div");
        el.innerHTML = html;
        var frag = document.createDocumentFragment(), node, lastNode;
        while ( (node = el.firstChild) ) {
            lastNode = frag.appendChild(node);
        }
        var firstNode = frag.firstChild;
        range.insertNode(frag);

        // Preserve the selection
        if (lastNode) {
            range = range.cloneRange();
            if (returnInNode)
            {
                range.setStart(lastNode, 0); //this part is edited, set caret inside pasted node.
            }
            else
            {
                range.setStartAfter(lastNode); 
            }
            if (selectPastedContent) {
                range.setStartBefore(firstNode);
            } else {
                range.collapse(true);
            }
            sel.removeAllRanges();
            sel.addRange(range);
        }
    }
} else if ( (sel = document.selection) && sel.type != "Control") {
    // IE < 9
    var originalRange = sel.createRange();
    originalRange.collapse(true);
    sel.createRange().pasteHTML(html);
    if (selectPastedContent) {
        range = sel.createRange();
        range.setEndPoint("StartToStart", originalRange);
        range.select();
    }
}

}

代码的作用:

  1. 有人使用 ctrl-v、上下文菜单或其他方式触发粘贴事件
  2. 在粘贴事件中,创建了一个具有 contenteditable 的新元素(具有 contenteditable 的元素具有提升的权限)
  3. 目标元素的插入符号位置已保存。
  4. 焦点设置在新元素上
  5. 内容被粘贴到新元素中并在 DOM 中呈现。
  6. 突变观察者捕捉到了这一点(它记录了对 dom 树和内容的所有更改)。然后触发突变事件。
  7. 粘贴内容的 dom 被克隆到一个变量中并返回给回调。临时元素被销毁。
  8. 回调接收克隆的 DOM。插入符号已恢复。您可以在将其附加到目标之前对其进行编辑。元素。在此示例中,我使用 Tim Downs 函数来保存/恢复插入符号并将 HTML 粘贴到元素中。

非常感谢Tim Down

【讨论】:

    【解决方案3】:

    类似这样的:

    $("yourTextAres").bind('paste', function() { 警报($(this).val()); });

    【讨论】:

    • 我没有投反对票,但这不会告诉你粘贴了什么,它只会告诉你该字段的当前值 - 粘贴不一定会替换该字段中的任何内容。 (假设您的浏览器甚至支持“粘贴”事件,但并非全部都支持。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多