如何在Meteor的body元素中添加CSS类?
问题描述:
我开始一个Meteor项目,我想在不同的网页上使用不同的body CSS类。如果我添加一个css类给body,我得到:
I'm starting a Meteor project and I want to use different body css classes on different pages. If I add a css class to body I get:
Attributes on <body> not supported
我发现的唯一方法是使用JS添加类。有没有更好的方法来做这个?
The only way I've found is adding the class using JS. Is there a better way to do this?
答
标准做法是在相应的路径挂钩中设置body类:
The standard practice is to set body class in respective path hooks:
Router.map(function() {
this.route('someRoute', {
path: '/someAddress',
onBeforeAction: function() {
$('body').addClass('someRouteBodyClass');
this.next();
},
onStop: function() {
$('body').removeClass('someRouteBodyClass');
},
...
};
});