当前位置:首页 > JavaScript

js实现列表效果

2026-02-02 17:27:32JavaScript

实现基础列表渲染

使用JavaScript动态渲染列表数据到HTML页面。假设有一个数组数据需要展示为无序列表:

const items = ['Apple', 'Banana', 'Orange'];
const listContainer = document.getElementById('list-container');

function renderList(items) {
  const ul = document.createElement('ul');

  items.forEach(item => {
    const li = document.createElement('li');
    li.textContent = item;
    ul.appendChild(li);
  });

  listContainer.appendChild(ul);
}

renderList(items);

HTML部分需要有一个容器元素:

<div id="list-container"></div>

添加交互功能

为列表项添加点击事件处理,实现选中效果:

function renderInteractiveList(items) {
  const ul = document.createElement('ul');

  items.forEach(item => {
    const li = document.createElement('li');
    li.textContent = item;
    li.addEventListener('click', function() {
      this.classList.toggle('selected');
    });
    ul.appendChild(li);
  });

  listContainer.innerHTML = '';
  listContainer.appendChild(ul);
}

CSS样式:

js实现列表效果

.selected {
  background-color: #f0f0f0;
  font-weight: bold;
}

实现动态列表操作

添加表单输入和按钮,支持动态添加列表项:

const addButton = document.getElementById('add-item');
const newItemInput = document.getElementById('new-item');

addButton.addEventListener('click', function() {
  const newItem = newItemInput.value.trim();
  if(newItem) {
    items.push(newItem);
    renderInteractiveList(items);
    newItemInput.value = '';
  }
});

HTML部分:

js实现列表效果

<input type="text" id="new-item" placeholder="Enter new item">
<button id="add-item">Add Item</button>
<div id="list-container"></div>

使用模板引擎

对于复杂列表结构,可以使用模板字符串或模板引擎:

function renderListWithTemplate(items) {
  const html = items.map(item => `
    <li class="list-item">
      <span>${item.name}</span>
      <span class="price">$${item.price}</span>
    </li>
  `).join('');

  listContainer.innerHTML = `<ul>${html}</ul>`;
}

const products = [
  { name: 'Laptop', price: 999 },
  { name: 'Phone', price: 699 },
  { name: 'Tablet', price: 399 }
];

renderListWithTemplate(products);

实现排序和过滤

为列表添加排序和过滤功能:

function sortList(order = 'asc') {
  const sortedItems = [...items].sort((a, b) => {
    return order === 'asc' 
      ? a.localeCompare(b)
      : b.localeCompare(a);
  });
  renderInteractiveList(sortedItems);
}

function filterList(searchTerm) {
  const filteredItems = items.filter(item => 
    item.toLowerCase().includes(searchTerm.toLowerCase())
  );
  renderInteractiveList(filteredItems);
}

HTML部分添加控制元素:

<input type="text" id="search" placeholder="Search items">
<button onclick="sortList('asc')">Sort A-Z</button>
<button onclick="sortList('desc')">Sort Z-A</button>
<div id="list-container"></div>

这些方法展示了如何使用JavaScript实现各种列表效果,从基础渲染到交互功能、动态操作和数据处理。根据具体需求,可以组合或扩展这些方法来实现更复杂的列表功能。

标签: 效果列表
分享给朋友:

相关文章

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-c…

vue实现过渡效果

vue实现过渡效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。以…

vue实现传输列表

vue实现传输列表

Vue 实现传输列表 在 Vue 中实现传输列表功能,通常涉及两个列表之间的数据传递,用户可以通过按钮或拖拽操作将项目从一个列表移动到另一个列表。以下是实现传输列表的几种方法。 使用组件间数据传递…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…

vue 实现toggle效果

vue 实现toggle效果

使用 v-show 实现 Toggle 通过 v-show 指令可以控制元素的显示与隐藏,适合频繁切换的场景。v-show 仅切换 CSS 的 display 属性,不会销毁和重建 DOM。 <…