File tree Expand file tree Collapse file tree 1 file changed +23
-19
lines changed Expand file tree Collapse file tree 1 file changed +23
-19
lines changed Original file line number Diff line number Diff line change 1
- . year - filter {
2
- margin - bottom : 20 px ;
3
- }
4
- . year - filter button {
5
- margin : 5 px ;
6
- padding : 10 px ;
7
- cursor : pointer ;
8
- border: 1 px solid #ccc;
9
- background - color : #f9f9f9;
10
- }
11
- . year - filter button . active {
12
- background - color : #007 bff ;
13
- color: white ;
14
- }
15
- . publication li {
16
- display: none ; /* 默认隐藏所有文章 */
17
- }
18
- . publication li . show {
19
- display: block ; /* 显示符合条件的文章 */
1
+ function filterPublications ( year ) {
2
+ const publications = document . querySelectorAll ( '.publication li' ) ;
3
+ const buttons = document . querySelectorAll ( '.year-filter button' ) ;
4
+
5
+ // 移除所有按钮的 active 类
6
+ buttons . forEach ( button => button . classList . remove ( 'active' ) ) ;
7
+
8
+ // 为当前点击的按钮添加 active 类
9
+ const activeButton = document . querySelector ( `.year-filter button[onclick="filterPublications('${ year } ')"]` ) ;
10
+ activeButton . classList . add ( 'active' ) ;
11
+
12
+ // 遍历所有文章,根据年份显示或隐藏
13
+ publications . forEach ( publication => {
14
+ const publicationYear = publication . getAttribute ( 'data-year' ) ;
15
+ if ( year === 'all' || publicationYear === year ) {
16
+ publication . classList . add ( 'show' ) ;
17
+ } else {
18
+ publication . classList . remove ( 'show' ) ;
19
+ }
20
+ } ) ;
20
21
}
22
+
23
+ // 默认显示所有文章
24
+ filterPublications ( 'all' ) ;
You can’t perform that action at this time.
0 commit comments