[英]BackboneJS Add and remove class to clicked element
I have this Backbone App where I fetch a JSON file, which displays the alphabetletters, when clicking on one of those letters, it will display the relevant Artists starting with that letter. 我有一个Backbone App,我在其中获取一个JSON文件,该文件显示字母,当单击这些字母之一时,它将显示以该字母开头的相关Artist。 It works so far but what I want to achieve is to add some kind of active
-class to the clicked letter. 到目前为止,它仍然有效,但是我要实现的是在单击的字母中添加某种active
-class。 How can i achieve this? 我怎样才能做到这一点?
My View looks like this: 我的视图如下所示:
function (App, Backbone) {
var Alphabet = App.module();
var LetterView = Backbone.View.extend({
template: 'alphabetlist',
tagName: 'li',
serialize: function() {
return this.model.toJSON();
},
});
Alphabet.View = Backbone.View.extend({
tagName: 'ol',
className: 'alist',
initialize: function() {
this.listenTo(this.collection, 'all', this.render);
this.listenTo(this.collection, 'sync', this.onSync);
this.collection.fetch();
},
beforeRender: function() {
var self = this;
this.collection.each(function(item) {
self.insertView(new LetterView({model: item}))
})
},
});
Alphabet.AlphabetCollection = Backbone.Collection.extend({
url: 'js/json/AlphabetCollection.json'
});
return Alphabet;
}
So can anyone help me out? 有人可以帮我吗? Thanks in advance... 提前致谢...
EDIT 编辑
When I do: 当我做:
events: {
'click a' : 'toggle'
},
serialize: function() {
return this.model.toJSON();
},
toggle: function(event) {
this.$el.parent().addClass('test');
},
the <ol>
-element gets the class, but still not targetting the <li>
-element... <ol>
-element获取类,但仍不定位<li>
-element ...
Should be like this. 应该是这样的。
var LetterView = Backbone.View.extend({
template: 'alphabetlist',
tagName: 'li',
events : {
"click li" : "toggle"
},
serialize: function() {
return this.model.toJSON();
},
toggle : function(e){
var active = $(e.currentTarget);
active.addClass('active');
$('li').not(active).removeClass('active');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.