我有一個簡單的問題,就是向當前的菜單項添加一個類。將課程添加到當前的導航項
我有以下頁面結構:
- index.jade
- about.jade
- articles/
- _data.json
- index.jade
- blog-post-1.jade
- blog-post-2.jade
- contact.jade
現在,我創建了一個叫做類:.active
,它僅適用於根頁面(指數,約和接觸),但是當我點擊/articles
活動類未被添加到文章li
在菜單中。
我使用此代碼段從豎琴網站:
ul
li(class="#{ current.source == 'index' ? 'active' : '' }")
a(href="/") Home
li(class="#{ current.source == 'about' ? 'active' : '' }")
a(href="/about") About
li(class="#{ current.source == 'articles' ? 'active' : '' }")
a(href="/articles") Articles
li(class="#{ current.source == 'contact' ? 'active' : '' }")
a(href="/contact") Contact
無論我怎麼努力,我似乎無法得到.active
菜單類在/articles
也沒有任何文章頁的工作:articles/blog-post-1
或articles/blog-post-2
等等。
同樣在豎琴現場我看到,您可以添加:
{
path: ["articles", "hello-world"],
source: "hello-world"
}
但我不知道在哪裏添加。我將它添加到articles/_data.json
文件中,但沒有奏效。
謝謝你的快速回復肯尼斯但我仍然無法將.active類添加到/ articles中的帖子中!當我點擊/文章時,該課程是活躍的,但是當我點擊「博客-1」時,課程沒有通過?! – user5898548
嘗試註銷數據,以便您可以看到該頁面上實際存在的元數據是否與您的期望匹配: ''' pre:code = JSON.stringify(current,0,2) ''' ' – kennethormandy
最後我用:#{current.source =='blog-post-2'?'active':''} – user5898548