Home >Web Front-end >Vue.js >How to delete dom node in vue.js

How to delete dom node in vue.js

王林
王林Original
2021-09-29 16:44:218615browse

Vue.js method of deleting dom nodes: 1. v-for loops dom nodes, ensuring that each li has a delete button and binding the delete event; 2. Delete dom nodes, such as [this. datahistory.splice(this.$refs.li[index], 1)].

How to delete dom node in vue.js

The operating environment of this article: windows10 system, vue.js 2.9, thinkpad t480 computer.

Vue needs to go through two steps to delete the current dom node:

The first step: v-for loops the dom node, ensuring that each li has a delete button and binds the delete event

      <ul>
        <li ref="li" v-for="(item, index) in datahistory" :key="index">
        	{{item}}
        	<button @click="deleteFn">删除</button>
        </li>
      </ul>

Step 2: Delete dom node

deleteFn () {
        this.datahistory.splice(this.$refs.li[index], 1) 
        // 点击调用删除方法,在datahistory数组里删除index这个下标项,逗号后面的1就代表删除当前的1项
      }

Recommended learning: php training

The above is the detailed content of How to delete dom node in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn