微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用backbonejs视图,将“onload”事件附加到图像标签的最佳方法是什么?

我想在骨干js视图中为图像附加一个“onload”事件.我目前将其包含在“事件”中,作为“加载img”:“功能”,但并没有被解雇.

有什么建议吗?

解决方法

Backbone的事件处理基于 delegate,委托只能用于泡沫的事件.问题是负载事件不起泡;从 HTML5 specification

If the download was successful
Set the img element to the completely available state,update the presentation of the image appropriately,and fire a simple event named load at the img element.

simple event不会泡泡:

Firing a simple event named e means that an event with the name e,which does not bubble (except where otherwise stated) […]

所以你必须用这样的方式用手挂钩一个处理程序:

render: function() {
    var self = this;
    this.$el.append(some_html_with_img_elements);
    this.$el.find('img').on('load',function() { self.img_loaded() });
    return this;
}

演示:http://jsfiddle.net/ambiguous/c7wH2/

原文地址:https://www.jb51.cc/js/152647.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐