我正在我的网络应用中安装 Dropbox Chooser ( https://www.dropbox.com/developers/chooser ),示例代码为:
<input type="dropbox-chooser" name="selected-file" id="db-chooser"/>
<script type="text/javascript">
document.getElementById("db-chooser").addEventListener("DbxChooserSuccess",
function(e) {
alert("Here's the chosen file: " + e.files[0].link)
}, false);
</script>
但是,我宁愿使用 jQuery,所以我正在尝试:
$("#db-chooser").on('DbxChooserSuccess',function(event) {
alert("Here's the chosen file: " + event.files[0].link)
}
警报确实触发,但事件不包含 files[] 节点。我做了console.log,但找不到数据。有其他方法可以做到这一点吗?
最佳答案
当我刚接触这个 API 时,我也遇到过类似的问题。当时我找到了一个链接,让我了解了很多这方面的东西。 Here's the link to that.
链接所说的是,要访问 event.files[0]
,您必须 checkin event.originalEvent
,而不是 event
。因此,您的代码将如下所示:
$("#db-chooser").on('DbxChooserSuccess',function(event) {
alert("Here's the chosen file: " + event.originalEvent.files[0].link)
}
实际发生的情况是,当初始化事件时,jQuery 会复制 originalEvent
(JS 事件)的几乎所有属性,并使用自定义属性创建自己的 event
包装器。除此之外,它将 originalEvent
存储为属性,以便稍后可以访问一些遗漏的属性(例如您的情况下的 files[0]
)。 From the jQuery Site :
jQuery’s event system normalizes the event object according to W3C standards. The event object is guaranteed to be passed to the event handler. Most properties from the original event are copied over and normalized to the new event object.
这就是为什么您的 files[0]
属性在 jQuery 事件中不存在。希望这会有所帮助。
PS:该链接中有很多好东西。请检查一下:)
关于javascript - 在 JQuery 而不是 Javascript 中读取所选的 Dropbox 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475120/