我有一個骨幹模板來表示足球運動員列表。渲染完成後,我通過向其中插入其他模板來修改一些模板元素。視圖渲染()函數是:無法從Backbone模板獲取元素
render: function() {
that = this;
this.$el.html(template(this.model.attributes));
var homeTeam = new player.PlayerCollection({team_id: this.model.get('home_id')});
homeTeam.fetch({
data: { forward_first:true, exlude_keepers:true},
processData: true,
success:function (collection) {
var forwards = collection.where({'position':'Forward'});
new PlayerListGoalView({players: forwards,
position:"Forward",
el: $("#home-forwards", that.el),
player_id:that.player_id
});
}
});
this.prepareList();
},
生成的HTML有一系列的嵌套列表,例如:
<ul id="expList" class="topcoat-list list">
<li id="home-forwards">
<ul class="topcoat-list list">
<li class="topcoat-list__item " id="player-713">
<span>Fabio Borini</span>
</li>
<li class="topcoat-list__item " id="player-696">
<span>Jozy Altidore</span>
</li>
<li class="topcoat-list__item " id="player-697">
<span>Mikael Mandron</span>
</li>
</ul>
</li>
<li id="away-forwards">
</li>
<li id="home-midfielders">
</li>
<li id="away-midfielders">
</li>
<li id="home-defenders">
</li>
<li id="away-defenders">
</li>
</ul>
中prepareList(),我得到的主UL,然後它的li孩子有自己的ul:
prepareList: function(){
var ul = this.$('#expList');
var el = ul.find('li:has(ul)');
console.log('ul.length is:');
console.log(ul.length);
console.log('el.length is:');
console.log(el.length);
},
這導致「ul.length is:1」和「el.length is:0」。所以它得到了ul,但不是li。
當我在chrome檢查器中展開ul標記時,可以看到firstElementChild是li#home-forward,並且所有innerHTML都如上所示。但它仍然無法得到李。
現在,如果我將上面的HTML硬編碼到模板中,則會找到ul和li!
任何想法?
這是什麼:'$(「#home-forwards」,that.el)'for?不應該是'el:那。$(selector)'?或者可能'el:$(「#home-forwards」)' –