突出显示文本,删除它并打开新标签页

时间:2016-09-05 06:07:12

标签: javascript html

我试图通过突出显示文本并将其放在div中来打开新选项卡,使用函数创建带有删除文本的新变量,并使用新变量打开新选项卡。 我发现一些来源可以通过剪贴板粘贴来实现。

这里是jsFiddle

function handlePaste (e) {
        var clipboardData, pastedData;

        // Stop data actually being pasted into div
        e.stopPropagation();
        e.preventDefault();

        // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text');
    window.open('http://'+pastedData+'.com', '_blank');
    }

document.getElementById('editableDiv').addEventListener('paste', handlePaste);

1 个答案:

答案 0 :(得分:1)

你必须像这样修改它:

function handlePaste (e) {
    var clipboardData, pastedData;

    // Stop data actually being pasted into div
    e.stopPropagation();
    e.preventDefault();

    // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text');
    window.open('http://'+pastedData+'.com', '_blank');
}

document.getElementById('editableDiv').addEventListener('paste', handlePaste);

// Store dragged element
document.addEventListener("dragstart", function( event ) {
    // store a ref. on the dragged elem
    dragged = event.target;
}, false);

// Drop event listener
document.getElementById('editableDiv').addEventListener('drop', function(e) {
    e.stopPropagation();
    e.preventDefault();

    // dragged element (p tag) is a DOM node, so we have to take it's text content
    window.open('http://'+dragged.textContent+'.com', '_blank');
});

Demo