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=''">
精彩评论