当前位置:首页 > VUE

vue 实现添加删除 div

2026-01-22 13:02:05VUE

使用 Vue 实现动态添加和删除 Div

在 Vue 中动态添加和删除 Div 可以通过数据驱动的方式实现。以下是具体实现方法:

数据绑定与列表渲染

通过 v-for 指令渲染一个 Div 列表,并使用数组来管理这些 Div:

<template>
  <div>
    <button @click="addDiv">添加 Div</button>
    <div v-for="(item, index) in divList" :key="index">
      {{ item.content }}
      <button @click="removeDiv(index)">删除</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      divList: []
    }
  },
  methods: {
    addDiv() {
      this.divList.push({
        content: `Div ${this.divList.length + 1}`
      })
    },
    removeDiv(index) {
      this.divList.splice(index, 1)
    }
  }
}
</script>

使用唯一标识作为 key

为了提高渲染性能,建议为每个 Div 分配唯一 ID 作为 key:

<template>
  <div>
    <button @click="addDiv">添加 Div</button>
    <div v-for="item in divList" :key="item.id">
      {{ item.content }}
      <button @click="removeDiv(item.id)">删除</button>
    </div>
  </div>
</template>

<script>
let nextId = 1

export default {
  data() {
    return {
      divList: []
    }
  },
  methods: {
    addDiv() {
      this.divList.push({
        id: nextId++,
        content: `Div ${this.divList.length + 1}`
      })
    },
    removeDiv(id) {
      this.divList = this.divList.filter(item => item.id !== id)
    }
  }
}
</script>

添加动画效果

可以为添加和删除操作添加过渡动画:

<template>
  <div>
    <button @click="addDiv">添加 Div</button>
    <transition-group name="fade" tag="div">
      <div v-for="item in divList" :key="item.id" class="item">
        {{ item.content }}
        <button @click="removeDiv(item.id)">删除</button>
      </div>
    </transition-group>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
.item {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ddd;
}
</style>

使用组件封装

对于更复杂的 Div 内容,可以将其封装为组件:

<template>
  <div>
    <button @click="addDiv">添加 Div</button>
    <div v-for="item in divList" :key="item.id">
      <custom-div :content="item.content" @remove="removeDiv(item.id)" />
    </div>
  </div>
</template>

<script>
import CustomDiv from './CustomDiv.vue'

export default {
  components: {
    CustomDiv
  },
  data() {
    return {
      divList: []
    }
  },
  methods: {
    addDiv() {
      this.divList.push({
        id: Date.now(),
        content: `Div ${this.divList.length + 1}`
      })
    },
    removeDiv(id) {
      this.divList = this.divList.filter(item => item.id !== id)
    }
  }
}
</script>

CustomDiv.vue 中:

vue 实现添加删除 div

<template>
  <div class="custom-div">
    {{ content }}
    <button @click="$emit('remove')">删除</button>
  </div>
</template>

<script>
export default {
  props: {
    content: String
  }
}
</script>

这些方法提供了在 Vue 中实现动态添加和删除 Div 的不同方案,可以根据具体需求选择适合的方式。

标签: vuediv
分享给朋友:

相关文章

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现滚动显示

vue实现滚动显示

Vue 实现滚动显示的几种方法 在 Vue 中实现滚动显示可以通过多种方式实现,以下是几种常见的方法: 使用 CSS 动画 通过 CSS 的 animation 属性结合 @keyframes 实现…