javascript - 在 JQuery 而不是 Javascript 中读取所选的 Dropbox 响应

标签 javascript jquery selector dropbox dropbox-api

我正在我的网络应用中安装 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/

相关文章:

javascript - 如何通过 javascript 设置 -webkit-mask-box-image?

jquery - 使用 geocomplete 将结果限制为国家和州

javascript - 如何将用户名和密码从输入值安全地传递到reactjs中的redux store/reducer?

javascript - 我想使用 for 循环来获取 jquery 中复选框和文本框的 id

javascript - 使用 jQuery 动态更改链接

objective-c - 如何将 SEL 传递给 dispatch_async 方法

javascript - 按钮的 Jquery 选择器

iOS - 计时器选择器线程

javascript - HTML5 canvas 基于滚动的动画吸引和脱离

javascript - 如何将 Cognito 与生成的 Javascript SDK 结合使用?