admin管理员组文章数量:1291300
When using jQuery's .trigger('dragstart') to fire off a function on an element with the attribute ondragstart="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');});
- ondragstart="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:
<div class="l-col-container">
<section id="1130" class="orderqueue">
<h1>Order Queue</h1>
<div class="orderList">
<div id="100" class="order" timeslot="1130" ordernumber="100" draggable="true" ondragstart="drag(event)">
<div class="order-name">Johnny Cash</div>
<div class="order-num">Order Number: 100</div>
</div>
<!-- /order -->
<div id="101" class="order" timeslot="1130" ordernumber="101" draggable="true" ondragstart="drag(event);">
<div class="order-num">101</div>
<div class="order-name">Johnny Cash</div>
</div>
<!-- /order -->
</div>
<!-- /orderlist -->
</section>
<div class="queuebar">
<h2>Queue Bar</h2>
<hr/>
<div class="queuebar-dropzone"></div>
</div>
<div id="testsensor">
<h2>Test Log</h2>
<p id="testsensor-output"></p>
</div>
</div>
<!-- /l-col-container -->
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");
}
When using jQuery's .trigger('dragstart') to fire off a function on an element with the attribute ondragstart="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');});
- ondragstart="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:
<div class="l-col-container">
<section id="1130" class="orderqueue">
<h1>Order Queue</h1>
<div class="orderList">
<div id="100" class="order" timeslot="1130" ordernumber="100" draggable="true" ondragstart="drag(event)">
<div class="order-name">Johnny Cash</div>
<div class="order-num">Order Number: 100</div>
</div>
<!-- /order -->
<div id="101" class="order" timeslot="1130" ordernumber="101" draggable="true" ondragstart="drag(event);">
<div class="order-num">101</div>
<div class="order-name">Johnny Cash</div>
</div>
<!-- /order -->
</div>
<!-- /orderlist -->
</section>
<div class="queuebar">
<h2>Queue Bar</h2>
<hr/>
<div class="queuebar-dropzone"></div>
</div>
<div id="testsensor">
<h2>Test Log</h2>
<p id="testsensor-output"></p>
</div>
</div>
<!-- /l-col-container -->
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");
}
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Sep 4, 2014 at 17:14
hollerholler
931 silver badge10 bronze badges
3 Answers
Reset to default 8See jquery html 5 dragstart .on('dragstart',function(e){}) e.dataTransfer.setData() doesn't work
using the event.originalEvent.dataTransfer
does the trick
Try this
HTML
<div id="draggable" ></div>
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
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
本文标签:
版权声明:本文标题:javascript - How to .setData.getData when using jQuery's .trigger('dragstart') for HTML5's drag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530208a2383712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论