我想出了以下代码,但问题是,每个菜单项都会有重复的 anchor 标记。有没有更好的方法来做到这一点?
ul.nav
- if(menu="Home")
li.active
a(href="#") Dashboard
else
li
a(href="#") Dashboard
li
a(href="#") About
li
a(href="#") Contact
最佳答案
在另一个 question that was similar 中找到了这个:
在每个“li”处使用一个三元组
ul
li(class=(title === 'Home' ? 'active' : ''))
a(href='#') Home
li(class=(title === 'Dashboard' ? 'active' : ''))
a(href='#') Dashboard
如果需要,您可以设置路由以传递“菜单”值而不是使用“标题”:
exports.index = function(req, res) {
res.render('index', {title: 'Home', menu: 'Home'});
}
关于node.js + Jade + express : How can I create a navigation that will set class active if the path matches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10713923/