Vue中如何點(diǎn)擊獲取當(dāng)前元素下標(biāo)
vue點(diǎn)擊獲取當(dāng)前元素下標(biāo)
項(xiàng)目開發(fā)中,固定列表是很常見的,有時(shí)候我們需要得到元素的下標(biāo)去做一些事情,但是少了v-for想要獲取下標(biāo)就比較困難了
下面我便介紹我的方法
其實(shí)我用的方法很簡單,就是用事件委托機(jī)制去獲取事件發(fā)生的元素,再獲取父元素的子元素集合,再遍歷子元素集合,當(dāng)事件發(fā)生元素等于子元素集合中的某一個(gè)時(shí),返回?cái)?shù)組下標(biāo),即可得到事件發(fā)生元素的下標(biāo)。
<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;//同級(jí)元素集合
? ? ? ? ? ? let targetIndex=_this.tabIndex(target,nodeList);//調(diào)用tabIndex方法,返回值便是元素下標(biāo)
? ? ? ? ? ? console.log(targetIndex)//打印當(dāng)前點(diǎn)擊元素的下標(biāo)
? ? ? ? ? })
? ? ? ? },
? ? ? ? methods: {
? ? ? ? ? /**
? ? ? ? ? ?*
? ? ? ? ? ?* @param target 事件觸發(fā)的元素
? ? ? ? ? ?* @param nodeList 元素集合
? ? ? ? ? ?* @returns {number} 返回事件觸發(fā)元素的下標(biāo)
? ? ? ? ? ?*/
? ? ? ? ? tabIndex(target,nodeList){
? ? ? ? ? ? for(let i=0;i<nodeList.length;i++){
? ? ? ? ? ? ? if(target===nodeList[i]){
? ? ? ? ? ? ? ? return i;
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? }
</script>
<style scoped>
</style>vue數(shù)組獲取下標(biāo)的坑
使用Vue 編寫了基礎(chǔ)的添加刪除功能遇到了一個(gè)小坑
<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>刪除功能遇到點(diǎn)小坑,每次點(diǎn)擊刪除都會(huì)刪除第一條內(nèi)容

后來發(fā)現(xiàn)在下面代碼處index并未獲取還并未報(bào)錯(cuò)
<td><button @click="Delete_person(index)">刪除</button></td>
// Vue 代碼
Delete_person: function(index) {
this.people.splice(index, 1); //splice 命令如果傳入下標(biāo)將默認(rèn)刪除第一條
console.log(index)
}
}由此從以下代碼中查找到獲取下標(biāo)的方法
<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;
//獲取點(diǎn)擊對象
var el = event.currentTarget;
console.log("當(dāng)前對象的內(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解vue-router如何實(shí)現(xiàn)動(dòng)態(tài)路由
在構(gòu)建基于Vue.js的單頁面應(yīng)用(SPA)時(shí),Vue?Router是一個(gè)不可或缺的工具,本文將詳細(xì)介紹動(dòng)態(tài)路由的概念與作用及其在Vue?Router中的具體實(shí)現(xiàn),需要的可以參考下2024-11-11
Vue首頁界面加載優(yōu)化實(shí)現(xiàn)方法詳解
如果你也遇到在 vue 應(yīng)用中,首頁加載資源過多,導(dǎo)致加載緩慢的問題,下面的方法也許能幫到你,主要的處理思想是:讓首頁多余的資源能在需要它的時(shí)候再加載2022-09-09
vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解
我們在做列表查詢的時(shí)候,會(huì)有很多查詢項(xiàng),如何實(shí)現(xiàn)超過n行查詢項(xiàng)的時(shí)候自動(dòng)折疊起來呢?本文給大家分享vue3實(shí)現(xiàn)搜索項(xiàng)超過n行就折疊的思路詳解,感興趣的朋友一起看看吧2022-06-06
Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字的過程
這篇文章主要介紹了Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

