html5 setdata,How to .setData/.getData when using jQuery's .trigger('dragstart') for HTML5's drag an...

本文探讨了使用jQuery触发HTML5 dragstart事件时遇到的问题,即通过touchmove事件触发dragstart后无法正确使用setData方法。文章提供了几种解决方案,包括使用event.originalEvent.dataTransfer以及完全避免使用HTML5拖放API。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

When using jQuery's .trigger('dragstart') to fire off a function on an element with the attribute οndragstart="drag(event)" , how do you .setData/.getData for HTML5's drag and drop on touch device?**

Example flow:

User drags element with touchmove event

element the two following attributes:

.bind('touchmove', function(event){$(this).trigger('dragstart');});

οndragstart="drag(event)"

the drag(event) attempts to use event.dataTransfer.setData("Text", event.target.id);

the result is the following error.

"Uncaught TypeError: Cannot read property 'setData' of undefined"

For mouse event ondragstart, .setData/.getData call in the drag(event) function works fine. However, when firing the ondragstart via .trigger() the .setData/.getData methods do not work properly and an error is returned

Why do I get the following error when attempting to drag an order; and how do I fix it?**

"Uncaught TypeError: Cannot read property 'setData' of undefined"

Please refrain from directing towards using a .js library such as TouchPunch etc.

I'm trying to bind the 'touchmove' event to fire off the drag(event) and provide the same .setData/.getData functionality that is available for the mouse drag triggered event.

--->jsFiddle Link

HTML:

Order Queue

Johnny Cash
Order Number: 100
101
Johnny Cash

Queue Bar


Test Log

JavaScript:

var orders = $('.order');

var testelement = document.getElementById('testsensor-output');

var index = 0;

orders.each(function () {

$(this).bind('touchmove', function (evt) {

index++;

testelement.innerHTML = index + ' dragstart fired';

console.log('dragstart fired');

$(this).trigger('dragstart');

});

$(this).bind('dragstart', function(event){

drag(event);

});

});

function drag(ev) {

console.log(ev);

var obj = $('#' + ev.target.id);

ev.dataTransfer.setData("Text", ev.target.id);

var data = ev.dataTransfer.getData("Text");

}

回答1:

See jquery html 5 dragstart .on('dragstart',function(e){}) e.dataTransfer.setData() doesn't work

using the event.originalEvent.dataTransfer does the trick

回答2:

Try this

HTML

CSS

#draggable {width:50px;height:50px;background-color:#f00;position:absolute}

HTML5 Touch Event Handlers

var draggable = document.getElementById('draggable');

draggable.addEventListener('touchmove', function(event) {

var touch = event.targetTouches[0];

// Place element where the finger is

draggable.style.left = touch.pageX-25 + 'px';

draggable.style.top = touch.pageY-25 + 'px';

event.preventDefault();

}, false);

Thank you

回答3:

What I was trying to do here is impossible. I can't remember where I found the documentation, however the conclusion I arrived is that when setting/getting data via the HTML5 drag and drop method the event that fires the .setData much be a true mouse drag in order to transfer data. Another event cannot trigger a drag event and successfully .setData due to security restrictions.

I maybe incorrect, but for anyone else attempting the same... I suggest finding another method. I personally rewrote the code to entirely remove HTML5 drag and drop from the mix. It was an unhappy road to go down. :D

来源:https://stackoverflow.com/questions/25671377/how-to-setdata-getdata-when-using-jquerys-triggerdragstart-for-html5s-d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值