简体   繁体   English

在DIV上拖放文本

[英]Drag-drop text over DIV

I have a div in my website, and when it's clicked upon, it reveals the search box (done with jQuery). 我的网站上有一个div,当点击它时,它会显示搜索框(使用jQuery完成)。

However, I would like this div to accept drag'n'dropped text. 但是,我希望这个div能够接受拖拽的文字。 In my use case, a user selects regular text from anywhere on the site, and drag'n'drops it to copy-paste it into the search box. 在我的用例中,用户从站点的任何位置选择常规文本,然后拖放它以将其复制粘贴到搜索框中。

If the search box was always visible, he could simply drop it into the text box, handled natively by the browser/OS. 如果搜索框始终可见,他可以简单地将其放入文本框中,由浏览器/操作系统本机处理。 However, is there a way I could simulate the same thing with this div? 但是,有没有办法用这个div模拟同样的东西? The user would drop his text onto the div, and it would fire the click event to show the text box and paste the dropped text into the box. 用户将他的文本放到div上,它会触发click事件以显示文本框并将删除的文本粘贴到框中。

My website is uses Modernizr+jQuery+jQuery UI and HTML5/CSS3. 我的网站使用Modernizr + jQuery + jQuery UI和HTML5 / CSS3。 IE6 compatibility is a non-issue. IE6兼容性不是问题。

Thank you in advance! 先感谢您!

You can use the HTML5 Drag and Drop events: 您可以使用HTML5拖放事件:

$('#dropTextHere').bind('drop', function (e) {
    var theDroppedText = e.originalEvent.dataTransfer.getData('Text');
});

You can read more about it here . 你可以在这里阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM