简体   繁体   English

将自定义数据类型拖放到文本框中

[英]Drag and drop custom data type into text boxes

I am working on a feature which utilizes HTML5 drag and drop capabilities. 我正在开发一项利用HTML5拖放功能的功能。 This feature only needs to work in Firefox, so I'm not worried about cross-browser capabilities. 此功能仅需要在Firefox中工作,因此我不必担心跨浏览器功能。 Here's the problem that I have encountered: I need to be able to drag my data only into the text and password fields on the page. 这是我遇到的问题:我只需要将数据拖到页面上的text和password字段中即可。 If I use regular text/plain data type, it works fine, but it also allows for dragging of data into the search box, which is unacceptable, because some of the data that I'm dragging is sensitive (passwords). 如果我使用常规的text / plain数据类型,它可以很好地工作,但是它也允许将数据拖动到搜索框中,这是不可接受的,因为我拖动的某些数据是敏感的(密码)。 An alternative approach is to use a custom data type, but when I do this, my text and password fields stop accepting the drop. 另一种方法是使用自定义数据类型,但是当我这样做时,我的文本字段和密码字段将停止接受删除。 In a nutshell my code right now looks like this: 简而言之,我的代码现在看起来像这样:

document.getElementById('#init').addEventListener('dragstart', function(e){
    e.dataTransfer.setData('text/link-uri', 'www.test.com');
    e.dataTransfer.setData('text/sensitive', e.target.dataset.value);
    e.dataTransfer.effectAllowed = 'copy';
});  
window.addEventListener('dragenter', function(e){
    if (e.target.type !== 'text' || e.target.type !== 'password') {
       e.preventDefault();
    }
});
window.addEventListener('dragover', function(e){
    if (e.target.type !== 'text' || e.target.type !== 'password') {
       e.preventDefault();
    }
});
window.addEventListener('drop', function(e){
    var data = e.dataTarnsfer.getData('text/sensitive');
    e.target.value = data;
});

Is there a workaround here? 这里有解决方法吗? I somehow need to disallow the ability to drag into the searchbox. 我某种程度上需要禁止拖动到搜索框中的功能。 So, I need to either exclude the searchbox from the list of allowable targets with text/plain data type or make the text/password fields understand a new data type. 因此,我需要从具有文本/纯数据类型的允许目标列表中排除搜索框,或者使文本/密码字段理解新的数据类型。 Any pointers would be highly appreciated. 任何指针将不胜感激。 Thanks! 谢谢! Luka 卢卡

OK, some after some poking around I was able to overcome this issue. 好吧,经过一番摸索,我能够克服这个问题。 For anyone who is interested, I basically set the text/plain so some bogus data, and only used my custom data type in the drop handler. 对于感兴趣的任何人,我基本上都将文本/纯文本设置为一些虚假数据,并且仅在放置处理程序中使用我的自定义数据类型。 This way, for my forms, it would always use actual values, but when I would drag the value to the searchbox, it'd pick up the bogus data. 这样,对于我的表单,它将始终使用实际值,但是当我将值拖到搜索框时,它将选择伪造的数据。

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

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