使用backbonejs观点,什么是附加一个&QUOT的最佳方式;的onload"事件图像标记? [英] Using backbonejs view, what is the best way to attach an "onload" event to an image tag?

查看:135
本文介绍了使用backbonejs观点,什么是附加一个&QUOT的最佳方式;的onload"事件图像标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要附加一个onload事件在一个backbonejs视图图像。我现在把它包含在事件为负载IMG:功能,但它没有被解雇过

I want to attach an "onload" event for an image in a backbonejs view. I am currently including it in the "events" as "load img":"function", but it is not getting fired off.

这样做有什么建议?

推荐答案

骨干的事件处理是基于 委托 委托只能与事件冒泡使用。问题是,加载事件不冒泡;从 HTML5规范

Backbone's event handling is based on delegate and delegate can only be used with events that bubble. The problem is that load events do not bubble; from the HTML5 specification:

如果下载成功结果
  在 IMG 元素设置为完全可用状态,适当地更新图像的presentation和火命名为一个简单的事件负荷 IMG 元素。

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.

和一个简单的事件不起泡:

And a simple event does not bubble:

烧制简单的事件命名的电子意味着,与不起泡(除非另有说明)这个名字的电子的,活动[ ...]

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

所以你必须挂钩手工处理程序的东西是这样的:

So you'll have to hook up a handler by hand with something like this:

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/

这篇关于使用backbonejs观点,什么是附加一个&QUOT的最佳方式;的onload"事件图像标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆