我想在骨干js视图中为图像附加一个“onload”事件.我目前将其包含在“事件”中,作为“加载img”:“功能”,但并没有被解雇.
有什么建议吗?
解决方法
Backbone的事件处理基于
delegate
,委托只能用于泡沫的事件.问题是负载事件不起泡;从
HTML5 specification:
If the download was successful
Set theimg
element to the completely available state,update the presentation of the image appropriately,and fire a simple event namedload
at theimg
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; }