当前位置:首页 > 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样式:

.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部分:

<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部分添加控制元素:

js实现列表效果

<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 实现对话框效果 在 Vue 中实现对话框效果可以通过组件化方式完成,通常结合 v-if/v-show 控制显示隐藏,以及插槽(slot)实现内容自定义。 基础对话框组件实现 创建一个名…

vue列表查询实现

vue列表查询实现

实现Vue列表查询功能 基本数据绑定与渲染 在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。 <template> &l…

vue实现列表循环

vue实现列表循环

列表循环的实现方式 在Vue中实现列表循环主要依赖于v-for指令,该指令基于源数据多次渲染元素或模板块。 基础数组循环 使用v-for遍历数组时,语法为item in items或(item, i…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue 实现滑动效果

vue 实现滑动效果

Vue 实现滑动效果的方法 使用 CSS Transition 通过 Vue 的 v-if 或 v-show 结合 CSS Transition 实现滑动效果。定义一个 CSS 类,利用 transf…