在vue中獲取當前點擊元素的方法:1.新建html項目,引入vue;2.插入div標簽,綁定click事件;3.插入腳本標記,寫入事件函數;
具體步驟如下:
1.首先,新建一個html項目,并在項目中引入vue;
<!DOCTYPE html><html>
<head>
<script src="./js/vue.js"></script>
<title></title>
</head>
<body>
</body>
</html>
2.vue引入好后,在項目中插入一個div標簽,并將click事件與v-on標記進行綁定;
<!DOCTYPE html><html>
<head>
<script src="./js/vue.js"></script>
<title></title>
</head>
<body>
<div id="btnClick">
<button v-on:click = "searchBtn()">A</button>
<button v-on:click = "searchBtn()">B</button>
<button v-on:click = "searchBtn()">C</button>
</div>
</body>
</html>
3.最后,click事件綁定好后,在項目中插入腳本標記并寫入事件函數即可獲取當前點擊的元素;
<!DOCTYPE html><html>
<head>
<script src="./js/vue.js"></script>
<title></title>
</head>
<body>
<div id="btnClick">
<button v-on:click = "searchBtn()">A</button>
<button v-on:click = "searchBtn()">B</button>
<button v-on:click = "searchBtn()">C</button>
</div>
<script>
let v =new Vue({
el:"#btnClick",
methods:{
searchBtn:function(btn){
alert(btn);
console,dir(btn):
}
}
});
</script>
</body>
</html>