30

chrome throw err: Cannot call method 'setData' of undefined, I find the e is not equal to window.event(it havn't propert dataTransfer);both has very big different

I find both almost equal in the click event.

I used http://code.jquery.com/jquery-latest.js.

I don't use drag feature,I just want know why. it is new feature in html 5,jquery still behind of it ?. or jquery team don't want support it?? or some other reason

zsytssk
  • 702
  • 1
  • 6
  • 14

2 Answers2

70

In the callback function you get a jQuery wrapper over a native event. Use the originalEvent property of passed argument:

$('...').on('dragstart', function (event) {
    event.originalEvent.dataTransfer.setData('...', '...');
});

P.S. dont' forget to set the draggable="true" attribute for the element to be dragged.

John Doe
  • 4,574
  • 2
  • 26
  • 30
  • thank you very much. why jQuery doesn't just callback native event – zsytssk Dec 20 '12 at 02:05
  • 2
    @zsytssk It is done to provide cross-browser predictable contents and behaviour. Read this http://api.jquery.com/category/events/event-object/ – John Doe Dec 20 '12 at 10:47
  • would `event.dataTransfer.setData()` work then if jQuery is not included on the page – automaticAllDramatic Jan 24 '14 at 11:15
  • 1
    @rizwaniqbal, yes it would, since it is a method on a native event obejct. Although, you'll have to add the 'dragstart' event listener to the element by using either 'addEventListener' method or 'ondragstart' property for it. See https://developer.mozilla.org/en-US/docs/DragDrop/Recommended_Drag_Types. – John Doe Apr 04 '14 at 00:18
2

jQuery creates it's own event object, and it doesn't have a dataTransfer property yet. This adds dataTransfer to the event object.

$.event.props.push('dataTransfer');

two7s_clash
  • 5,755
  • 1
  • 27
  • 44