从浏览器中拖动所选内容

Drag the selected content from browser

本文关键字:拖动 浏览器      更新时间:2023-09-26

我想知道有没有办法找出这一点,如果我们在浏览器中选择内容(例如单击和拖动时突出显示),请使用jquery知道在浏览器中选择了什么。

例如,我有一个表格,

我选择了(如单击并拖动时突出显示)该表格的一行,现在我想将其拖到输入中。当我拖动内容时,我是否知道我从浏览器中拖动内容的任何可能性。在这里,我没有使用任何jquery拖放表。是否可以拖放,如果可能的话,我可以举个例子。

提前谢谢。

下面介绍了如何将表格行的内容拖到文本输入中。

包括 jQuery 和 jQueryUI:

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

创建一个表和输入元素:

<table>
  <tr>
    <td>Dog</td>
  </tr>
  <tr>
    <td>Fish</td>
  </tr>
  <tr>
    <td>Cat</td>
  </tr>
</table>
<p>
  <input type="text" placeholder="Choose an animal" id="dropzone" />
</p>

添加以下 JS:

$("tr").draggable({
  appendTo: "body", 
  helper: "clone"
});
$("#dropzone").droppable({
  drop: function(event, ui) {
    $(this).val(ui.draggable.text().trim());
  }
});

在 drop 函数中,$(this) 引用了第一个元素被拖动到的元素,因此将其值设置为被拖放的元素的文本很简单。

演示