How do I prevent drag on a child, but allow drag on the parent?

前端 未结 6 1065
情深已故
情深已故 2020-12-07 00:33

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

6条回答
  •  孤城傲影
    2020-12-07 01:13

    If you want to truly cancel out the drag and make it unnoticeable to parent drag handlers, you need to both preventDefault and stopPropagation:

    Drag me! :)

    Without the stopPropagation, the parent ondragstart will still be called even if the default behavior will be prevented (event.defaultPrevented == true). If you have code in that handler that doesn't handle this case, you may see subtle bugs.

    You can of course put the JavaScript above inside element.addEventListener('dragstart', ...) too.

提交回复
热议问题