简体   繁体   English

HTML5拖放-使用jQuery进行事件处理

[英]HTML5 Drag&Drop - Event handling with jQuery

So I've stumbled upon this a several times and now I'm finally fed up with this topic. 因此,我几次无意中发现了这个问题,现在我终于受够了这个话题。 Searching and googleing about it confuses me every time and now I'll need to ask by myself here. 每次搜索和搜索它都会使我感到困惑,现在我需要在这里自己问。

I'm up to implement native HTML5 drag&drop in a web app. 我打算在Web应用程序中实现本机HTML5拖放。 It works fine in Chrome and in IE too (at least when I tried last time). 它在Chrome和IE中也能正常工作(至少在上次尝试时)。

The problem now is, that event binding via jQuery wont work out properly in Firefox, whereas it does so in Chrome! 现在的问题是,通过jQuery进行事件绑定在Firefox中无法正常进行,而在Chrome中却可以! This is my Code: 这是我的代码:

$(document).on('dragstart','.leistung', function(){
    cedamed.handlers.dragElement(event);
});

And this is my handler: 这是我的处理程序:

this.dragElement = function(event){
    var dataObj = {}; 
    dataObj.category = event.target.getAttribute('class');
    dataObj.description = event.target.getAttribute('description');
    dataObj.code0 = event.target.getAttribute('code0');
    dataObj.code1 = event.target.getAttribute('code1');
    dataObj.code2 = event.target.getAttribute('code2');
    event.dataTransfer.setData('Text',JSON.stringify(dataObj));
    console.log("dragging");
};

Works in Chrome, Firefox gives me the following error: 在Chrome中工作,Firefox给我以下错误:

ReferenceError: event is not defined

It points to the line with: 它指向带有以下内容的行:

cedamed.handlers.dragElement(event);

I have come across 'solutions' that involved the originalEvent-property of the event api, which is often supposed to make everything work fine in FF, but it does not at all in my case. 我遇到了涉及事件api的originalEvent-property的“解决方案”,通常应该使所有事情在FF中都能正常工作,但就我而言,这根本不起作用。 I made it work by setting the 'ondragstart'-attribute directly in the HTML, but shouldnt it work with 'jQuery.on'? 我通过直接在HTML中设置'ondragstart'属性来使其起作用,但是它不应该与'jQuery.on'一起使用吗?

I'm sorry, there are several questions to this topic out there, but I just dont get whats going wrong in this field. 抱歉,这个主题有几个问题,但是我只是不明白这个领域出了什么问题。 Can you please give me an insight, whats wrong in here? 您能给我一个见解吗,这是怎么了?

I found out I have to pass 'event' as an argument to the jQuery callback function in 'on' such as: 我发现我必须将'event'作为参数传递给'on'中的jQuery回调函数,例如:

$(document).on('dragstart','.leistung', function(event){
   cedamed.handlers.dragElement(event);
});

With usage of originalEvent in 'drageElement' I made it work finally. 通过在'drageElement'中使用originalEvent,我终于使它工作了。 Sorry... 抱歉...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM