Accept drag & drop of image from another browser window

前端 未结 6 1966
夕颜
夕颜 2020-12-23 09:44

In Javascript, I know how to set up a drag & drop target that accepts file uploads from the user\'s computer. How can I set up a drop target that accepts images that are

6条回答
  •  悲&欢浪女
    2020-12-23 10:20

    Some browsers use text/plain some use text/html as well

    This code should pull any text or image source url on the latest Chrome, FF on Mac and PC.

    Safari doesn't seem to give the URL so if someone knows how to get it let me know.

    I'm still working on IE.

    function getAnyText(theevent) {
    //see if we have anything in the text or img src tag
        var insert_text;
        var location = theevent.target;
        var etext;
        var ehtml;
        try {
                etext = theevent.dataTransfer.getData("text/plain");
        } catch (_error) {}
        try {
                ehtml = theevent.dataTransfer.getData("text/html");
        } catch (_error) {}
        if (etext) {
                insert_text = etext;
        } else if (ehtml) {
                object = $('
    ').html(ehtml).contents(); if (object) { insert_text = object.closest('img').prop('src'); } } if (insert_text) { insertText(insert_text,location); } }

提交回复
热议问题