简体   繁体   English

如何在 jQuery 可拖动 div 上选择文本?

[英]How do you make text selectable on a jQuery draggable div?

I made a div draggable using jQuery.我使用 jQuery 制作了一个可拖动的 div。 However, there is some text in that box, and I would still like to be able to copy and paste the text, but when I make an item draggable well when I click on the box it starts to move the box?但是,该框中有一些文本,我仍然希望能够复制和粘贴文本,但是当我单击该框时使一个项目可以很好地拖动时,它会开始移动该框吗?

Use the cancel option when making the element draggable.使元素可拖动时使用cancel选项

Prevents dragging from starting on specified elements.防止从指定元素开始拖动。

$('.selector').draggable({ cancel: '.text' }); 

It doesn't make sense to allow dragging AND selecting text on the same element.允许在同一元素上拖动和选择文本是没有意义的。 However, if you keep thinking it's necessary, two things come to my mind:但是,如果您一直认为这是必要的,我会想到两件事:

Solution 1:解决方案1:

You can add a "handler" that will be the only child receiving the mouse down/up events to drag the div.您可以添加一个“处理程序”,它将是接收鼠标向下/向上事件以拖动 div 的唯一子项。 For instance:例如:

<div id="draggable">
  <div class="handler"></div>
  <div class="text">This is a selectable text</div>
</div>

And in your JavaScript code:在您的 JavaScript 代码中:

var draggableDiv = $('#draggable');
draggableDiv.draggable({
  handle: $('.text', draggableDiv)
});

Solution 2:解决方案2:

You can disable the draggable thing when a user try to select text:当用户尝试选择文本时,您可以禁用可拖动的东西:

<div id="draggable">
  <div class="text">This is a text</div>
</div>

And in your JavaScript code:在您的 JavaScript 代码中:

var draggableDiv = $('#draggable').draggable();
$('.text', draggableDiv).mousedown(function(ev) {
  draggableDiv.draggable('disable');
}).mouseup(function(ev) {
  draggableDiv.draggable('enable');
});

When someone tries to select something in .text , the draggable process is disabled.当有人试图在.text中选择某些内容时,可拖动过程被禁用。

The first solution is the proper one.第一个解决方案是正确的。

(As talked by the other 2 answers, but if you need an practical example, here it is (if I understood correctly): ) (正如其他2个答案所说,但如果你需要一个实际的例子,这里是(如果我理解正确的话):)

jsfiddle: Draggable demo jsfiddle:可拖动的演示

The point is to:_重点是:_

  1. add an extra child <span> as an dragHandler , in the (parent) <span> that you want to drag.在要拖动的(parent) <span>中添加一个额外的child <span>作为dragHandler
<span class="commentNt" id="commentDraggable_123">~//? was it not &quot;single bounded context&quot; in choreography?<br/>
  <span class="dragHandler">Drag</span></span>
  1. for the parent <span> , use following code to register that child <span> for dragging对于parent <span> ,使用以下代码注册该child <span>以进行拖动

    • (dragging will now only be triggered in child <span> 's region). (现在将仅在child <span>的区域中触发拖动)。
$('#commentDraggable_123').draggable({ handle: '.dragHandler' }); 
  1. now, only dragging the child <span> , the whole parent <span> will then be dragged along with.现在,只拖动child <span> ,整个parent <span>将被一起拖动。

As for:至于:

  • Q: Can I do this without introducing another child <span> inside the parent <span> ?问:我可以在不在parent <span>中引入另一个child <span>的情况下执行此操作吗?

    • ie: Can I just drag the "blank area", then the whole parent <span> is dragged;即:我可以只拖动“空白区域”,然后整个parent <span>被拖动;

      while I drag on the "text area", the text can be selected instead of being dragged?当我在“文本区域”上拖动时,可以选择文本而不是拖动?

  • A: idk, I hope there is a way. A:idk,我希望有办法。

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

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