Vue中如何點擊獲取當前元素下標
vue點擊獲取當前元素下標
項目開發(fā)中,固定列表是很常見的,有時候我們需要得到元素的下標去做一些事情,但是少了v-for想要獲取下標就比較困難了
下面我便介紹我的方法
其實我用的方法很簡單,就是用事件委托機制去獲取事件發(fā)生的元素,再獲取父元素的子元素集合,再遍歷子元素集合,當事件發(fā)生元素等于子元素集合中的某一個時,返回數(shù)組下標,即可得到事件發(fā)生元素的下標。
<template> ? <div class="footer-tab"> ? ? <div class="tab-item">home</div> ? ? <div class="tab-item">second</div> ? ? <div class="tab-item">third</div> ? </div> </template> <script> ? ? export default { ? ? ? ? name: 'commonFooter', ? ? ? ? data() { ? ? ? ? ? ? return {} ? ? ? ? }, ? ? ? ? created() { ? ? ? ? }, ? ? ? ? mounted(){ ? ? ? ? ? let _this=this; ? ? ? ? ? //事件委托 ? ? ? ? ? document.querySelector(".footer-tab").addEventListener("click",function(e){ ? ? ? ? ? ? let target=e.target;//事件發(fā)生的元素 ? ? ? ? ? ? let nodeList=e.target.parentNode.children;//同級元素集合 ? ? ? ? ? ? let targetIndex=_this.tabIndex(target,nodeList);//調(diào)用tabIndex方法,返回值便是元素下標 ? ? ? ? ? ? console.log(targetIndex)//打印當前點擊元素的下標 ? ? ? ? ? }) ? ? ? ? }, ? ? ? ? methods: { ? ? ? ? ? /** ? ? ? ? ? ?* ? ? ? ? ? ?* @param target 事件觸發(fā)的元素 ? ? ? ? ? ?* @param nodeList 元素集合 ? ? ? ? ? ?* @returns {number} 返回事件觸發(fā)元素的下標 ? ? ? ? ? ?*/ ? ? ? ? ? tabIndex(target,nodeList){ ? ? ? ? ? ? for(let i=0;i<nodeList.length;i++){ ? ? ? ? ? ? ? if(target===nodeList[i]){ ? ? ? ? ? ? ? ? return i; ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? } ? ? } </script> <style scoped> </style>
vue數(shù)組獲取下標的坑
使用Vue 編寫了基礎的添加刪除功能遇到了一個小坑
<div id="app"> <fieldset> <legend> Create New Person </legend> <div> <label>姓名:</label> <input v-model="new_person.name"> </div> <div> <label>年齡:</label> <input v-model="new_person.age"> </div> <div> <label>性別:</label> <select v-model="new_person.sex"> <option value="男">男</option> <option value="女">女</option> </select> </div> <div id=""> <input type="button" name="" id="" value="創(chuàng)建" v-on:click="Create_person" /> </div> </fieldset> <table> <thead> <tr> <th>姓名</th> <th>年齡</th> <th>性別</th> <th>刪除</th> </tr> </thead> <tbody> <tr v-for="person in people"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> <td><button @click="Delete_person(index)">刪除</button></td> </tr> </tbody> </table> </div> </body>
Vue代碼
<script> var vm = new Vue({ el: "#app", data: { new_person: { name: '', age: 0, sex: '男', }, people: [{ name: '1', age: 0, sex: '', }, { name: '2', age: 0, sex: '', }, { name: '3', age: 0, sex: '', } ], }, methods: { Create_person: function() { this.people.push(this.new_person) this.new_person = { name: '', age: 0, sex: '男', } }, Delete_person: function(index) { this.people.splice(index, 1); console.log(index) } } }) </script>
刪除功能遇到點小坑,每次點擊刪除都會刪除第一條內(nèi)容
后來發(fā)現(xiàn)在下面代碼處index并未獲取還并未報錯
<td><button @click="Delete_person(index)">刪除</button></td>
// Vue 代碼 Delete_person: function(index) { this.people.splice(index, 1); //splice 命令如果傳入下標將默認刪除第一條 console.log(index) } }
由此從以下代碼中查找到獲取下標的方法
<li v-for="(item,index) in tabList" v-on:click="addClass(index,$event)" >{{item.title}}</li> data里面聲明: data() { return { tabList: [ { id: 0, title: "首頁1" }, { id: 1, title: "首頁2" }, { id: 2, title: "首頁3" } ], current:0 }; }, methods: { addClass: function(index,event) { this.current = index; //獲取點擊對象 var el = event.currentTarget; console.log("當前對象的內(nèi)容:"+el.innerHTML); console.log(this.current)
然后修改代碼
<tbody> <tr v-for="(person,index) in people"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> <td><button @click="Delete_person(index)">刪除</button></td> </tr> </tbody>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
一文詳解vue-router如何實現(xiàn)動態(tài)路由
在構建基于Vue.js的單頁面應用(SPA)時,Vue?Router是一個不可或缺的工具,本文將詳細介紹動態(tài)路由的概念與作用及其在Vue?Router中的具體實現(xiàn),需要的可以參考下2024-11-11Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字的過程
這篇文章主要介紹了Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06