有一个 HTML 文本区域.当本地文件被拖放到文本区域时,我能够捕捉到该事件.但是如何获取拖放文件的名称呢?(最后要修改并插入到textarea中.)
There is a HTML textarea. I'm able to catch that event when a local file is dragged and dropped onto the textarea. But how to obtain the name of the dropped file? (To be modified and inserted into the textarea finally.)
在这种情况下,以下表达式返回 None:
The following expressions returns None in that case:
event.dataTransfer.files
event.dataTransfer.getData('text/plain')
我为 Firefox 3 做了一个简短的示例,这是我目前的目标平台.
I made a short example for Firefox 3 that is my target platform currently.
<script>
function init() {
document.getElementById('x').addEventListener('drop', onDrop, true)
}
function onDrop(event) {
var data = event.dataTransfer.getData('text/plain')
event.preventDefault()
alert('files: ' + event.dataTransfer.files + ' && data: ' + data + '.')
}
</script>
<body onload='init()'>
<textarea cols=70 rows=20 id='x'></textarea>
这有点晚了 - 但我想你要找的是这个:
This is a bit late - but I think what you are looking for is this:
event.dataTransfer.files[0].name
您还可以获得以下属性:
You can also get the following properties:
event.dataTransfer.files[0].size
event.dataTransfer.files[0].type
您可以使用以下内容循环遍历这些文件:
And you can loop thru these files with the following:
var listOfNames='';
for(var i=0,tot=event.dataTransfer.files.length; i<tot; i++){
listOfNames+=event.dataTransfer.files[i].name + '
';
}
顺便说一句 - 如果你使用的是 jQuery,那么 dataTransfer 对象可以在这里找到:
Btw - if you are using jQuery then the dataTransfer object can be found here:
event.originalEvent.dataTransfer.files[0].name
这篇关于使用 HTML/JavaScript 检测本地文件拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 javascript 认为文档“准备好"之前,如何让How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 认为文档“准备好
jasmine 运行和等待实际上是做什么的?What do jasmine runs and waitsFor actually do?(jasmine 运行和等待实际上是做什么的?)
如何提供模拟文件来更改 <input type='filHow to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模拟文件来更改 lt;input type=filegt; 的事
如何使用 Jasmine 对链式方法进行单元测试How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
如何将 $rootScope 注入 AngularJS 单元测试?How do I inject $rootScope into an AngularJS unit test?(如何将 $rootScope 注入 AngularJS 单元测试?)
Jasmine - 如何监视函数中的函数调用?Jasmine - How to spy on a function call within a function?(Jasmine - 如何监视函数中的函数调用?)