Drag and Drop images from another website to mine

前端 未结 3 674
南旧
南旧 2020-12-14 01:12

I ran a quick google and SO search and found similar questions but none were well formed and most were old and looked abandoned (no answers, and no comments for a while). So

相关标签:
3条回答
  • 2020-12-14 01:49

    You can use this

    $(document).bind('drop', function (e) {        
        var url = $(e.originalEvent.dataTransfer.getData('text/html')).filter('img').attr('src');
        if (url) {
            jQuery('<img/>', {
                src: url,
                alt: "resim"
            }).appendTo('#yourId');            
        }
        return false;
    })
    
    0 讨论(0)
  • 2020-12-14 01:51

    Based on Yunus's answer , this seems to work in FF:

    e.originalEvent.dataTransfer.getData('text/html').match(/src\s*=\s*"(.+?)"/)[1]
    
    0 讨论(0)
  • 2020-12-14 02:06

    Try this: http://jsfiddle.net/2Jet2/70/

    $(document).on('dragover', function(e) {
         e.preventDefault();
    });
    $(document).on('drop', function(e) {
        e.preventDefault();
        e.originalEvent.dataTransfer.items[0].getAsString(function(url){
            alert(url);
        });
    });​
    

    I get "http://static3.flattr.net/thing/image/9/4/5/5/0/huge.png?1326712342" When I dragged that image from another browser window.

    .getAsString takes a callback which gets the url as argument once it's called

    Doesn't work on firefox

    0 讨论(0)
提交回复
热议问题