我无法弄清楚为什么视图内的事件的 jquery click 事件不会触发..
我有这样的主干视图:
class TreeList extends Backbone.View
el: "#tree-list"
events:
"click li span":"toggleTree"
"click li .select":"selectChannel"
template: _.template """
<ul id="tree"></ul>
"""
initialize: =>
$(@el).undelegate('li span', 'click');
$(@el).undelegate('li .select', 'click');
$("body").css "cursor", "progress"
@render()
render: =>
$(@el).html @template
_.each @collection.models, (model) =>
item = new TreeListItem model:model
$("#tree").append item.render()
if model.attributes.selected
console.log($("##{model.attributes.ChannelID} .select"))
$("##{model.attributes.ChannelID} .select")[0].click() # this event is never fired (selectChannel event)
return this
toggleTree: (e) =>
console.log "toggle tree"
selectChannel: (e) =>
console.log "selet channel"
如您所见,当我单击元素时,我在元素上调用 click 事件会触发toggleTree事件,但不是使用 jquery click 方法。
该元素的 console.log :
[i.icon-check-empty select, prevObject: x.fn.x.init[1], context: document, selector: "#d972c967-6b56-40b5-99c9-a25300c03341 .select", jquery: "1.10.2", constructor: function…]
将元素的背景设置为黑色确实有效..
$("##{model.attributes.ChannelID} .select").css
background:"#000"
任何人都可以阐明为什么会这样吗?