开发者

How do you capture an event for a URL dropped on an input box

开发者 https://www.devze.com 2023-03-12 16:24 出处:网络
I have a web form with a text input box for a url. When I drag a url from the browser\'s (I\'m using firefox but i\'d like it to work in others too if possible) address bar to the input box it inserts

I have a web form with a text input box for a url. When I drag a url from the browser's (I'm using firefox but i'd like it to work in others too if possible) address bar to the input box it inserts the url text into any pre-existing text. I want the url to overwrite any existing text instead.

I'm thinking I could capture some event and ease any existing text before adding the dropped text.

If it's possible to set up using jQuery that's nice, otherwise in simple javascript.

Edit

This seems to work:

$("#myinput").bind("drop", function(e) {  
      e.currentTarget.value = "";  
      return true;  
});

I开发者_JAVA技巧'm not sure what browsers support this event but if it's most modern ones then it should be fine.


If you are targeting ie,firefox 3.5+, safari, or chrome, the ondrop event should work. You can either use the form Dr.Molle supplied or you can set the ondrop handler with javascript.

//for ie must use this syntax or the html version
document.getElementById('myInput').ondrop = function(e){
    //doesn't have to be jquery here
    //using window.location because I can't test e.dataTransfer.getData('Text') right now
    $(this).val(window.location);
}


There is a ondrop-event firing:

<input ondrop="this.value=''">
0

精彩评论

暂无评论...
验证码 换一张
取 消