|
| 1 | +// 博客列表数据 |
| 2 | +const blogList = [ |
| 3 | + { |
| 4 | + title: '一些typing内置类型', |
| 5 | + content: '更注重于安全性、可维护性等特性的内置类型', |
| 6 | + icon: "https://www.devacademy.es/wp-content/uploads/2018/10/python-logo-1024x1024.png", |
| 7 | + link: '../../h5/effpy/typing的一些类型.html' |
| 8 | + } |
| 9 | +]; |
| 10 | + |
| 11 | +const blogListContainer= document.getElementById('blogListContainer'); |
| 12 | + |
| 13 | +// 初始化博客列表 |
| 14 | +function initBlogList() { |
| 15 | + blogListContainer.innerHTML = ''; |
| 16 | + blogList.forEach(blog => { |
| 17 | + const blogCard = document.createElement('div'); |
| 18 | + blogCard.className = 'blog-card'; |
| 19 | + // 添加链接 |
| 20 | + const link = document.createElement('a'); |
| 21 | + link.href = blog.link; |
| 22 | + // 创建博客图标元素 |
| 23 | + const icon = document.createElement('img'); |
| 24 | + icon.className = 'blog-icon'; |
| 25 | + icon.src = blog.icon; |
| 26 | + // 创建博客内容元素 |
| 27 | + const content = document.createElement('div'); |
| 28 | + content.className = 'blog-content'; |
| 29 | + // 创建博客标题元素 |
| 30 | + const title = document.createElement('div'); |
| 31 | + title.className = 'blog-title'; |
| 32 | + title.textContent = blog.title; |
| 33 | + // 创建博客描述元素 |
| 34 | + const description = document.createElement('div'); |
| 35 | + description.className = 'blog-description'; |
| 36 | + description.textContent = blog.content; |
| 37 | + // 将元素添加到DOM中 |
| 38 | + content.appendChild(title); |
| 39 | + content.appendChild(description); |
| 40 | + link.appendChild(icon); |
| 41 | + link.appendChild(content); |
| 42 | + blogCard.appendChild(link); |
| 43 | + blogListContainer.appendChild(blogCard); |
| 44 | + }); |
| 45 | +} |
| 46 | +// 搜索功能 |
| 47 | +function searchBlog() { |
| 48 | + const keyword = document.getElementById('searchInput').value; |
| 49 | + const blogCards = document.getElementsByClassName('blog-card'); |
| 50 | + Array.from(blogCards).forEach(blogCard => { |
| 51 | + const title = blogCard.querySelector('.blog-title').textContent; // 获取标题文本内容 |
| 52 | + const description = blogCard.querySelector('.blog-description').textContent; // 获取描述文本内容 |
| 53 | + if (title.includes(keyword) || description.includes(keyword)) { |
| 54 | + blogCard.style.display = 'flex'; |
| 55 | + // 关键字高亮显示 |
| 56 | + const regex = new RegExp(`(${keyword})(?![^<]*>|[^<>]*<\/a>)`, 'gi'); |
| 57 | + blogCard.innerHTML = blogCard.innerHTML.replace(regex, '<span class="highlight">$1</span>'); |
| 58 | + // console.log(blogCard.innerHTML); |
| 59 | + } else { |
| 60 | + blogCard.style.display = 'none'; |
| 61 | + } |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +// 返回按钮功能 |
| 67 | +function returnToAllBlogs() { |
| 68 | + const blogCards = document.getElementsByClassName('blog-card'); |
| 69 | + Array.from(blogCards).forEach(blogCard => { |
| 70 | + blogCard.style.display = 'flex'; |
| 71 | + blogCard.innerHTML = blogCard.innerHTML.replace('<span class="highlight">', '').replace('</span>', ''); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +// 添加搜索按钮点击事件 |
| 76 | +document.getElementById('searchButton').addEventListener('click', searchBlog); |
| 77 | + |
| 78 | +// 添加回车键事件,触发搜索功能 |
| 79 | +document.getElementById('searchInput').addEventListener('keydown', (event) => { |
| 80 | + if (event.key === 'Enter') { |
| 81 | + searchBlog(); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +// 添加返回按钮点击事件 |
| 86 | +document.getElementById('returnButton').addEventListener('click', returnToAllBlogs); |
| 87 | + |
| 88 | +// 初始化博客列表 |
| 89 | +initBlogList(); |
0 commit comments