如何防止拖曳孩子,但允许拖拽父母? [英] How do I prevent drag on a child, but allow drag on the parent?

查看:106
本文介绍了如何防止拖曳孩子,但允许拖拽父母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以拖动的div,里面的div是一些文本,我想允许用户选择(因此它们不能拖动)。如何允许div拖动,而不是跨度?



dragstart事件在div上。



<我可能忽略了一些简单的东西。我在div上尝试了draggable = true,在span上拖动drag = false。那没办法尝试在dragstart上返回false,这也不起作用。



dragstart(大致):

  var jTarget = $(e.target); 
if((jTarget.is('div.header')||(jTarget.parents('div.header'))
&!jTarget.is('a,input,span ')))
{
e.originalEvent.dataTransfer.setData(Text,test);
}
else
{
if(e.preventBubble)
e.preventBubble();
if(e.stopPropagation)
e.stopPropagation();
return false; // ???
}

if else部分按照我的预期工作,但是我无法停止拖动并允许选择。

解决方案

只是发布,以防有​​人用类似的问题搜索它

  element.addEventListener('mousedown',function(){this.parentNode.setAttribute(draggable,false);}); 
element.addEventListener('mouseup',function(){this.parentNode.setAttribute(draggable,true);});

为我工作


I have a div which the user can drag, inside that div is a span with some text which I want to allow the user to select (thus they cannot drag it). How do I allow the div to drag, but not the span?

The dragstart event is on the div.

I'm probably overlooking something simple. I tried draggable=true on the div, and draggable=false on the span. That didn't work. Tried returning false on dragstart, that didn't work either.

dragstart (roughly):

var jTarget = $(e.target);
if ((jTarget.is('div.header') || (jTarget.parents('div.header')) 
       && !jTarget.is('a, input, span'))) 
{
   e.originalEvent.dataTransfer.setData("Text", "test");
}
else
{
   if(e.preventBubble)
      e.preventBubble();
   if(e.stopPropagation)
      e.stopPropagation();
   return false;//???
}

The if else portion works as I expect, but I cannot get anything to stop the drag and allow the select.

解决方案

just posting in case someone searches for it with a similar Problem

element.addEventListener('mousedown', function() { this.parentNode.setAttribute("draggable", false); });
element.addEventListener('mouseup', function() { this.parentNode.setAttribute("draggable", true); });

worked for me

这篇关于如何防止拖曳孩子,但允许拖拽父母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆