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/

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...