v-for指令用法二
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title></title> <link rel="icon" type="image/ico" href="src/assets/favicon.ico"> <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"> <script src="src/assets/vue2.min.js"></script> </head> <body> <a v-for="(index,item) in items" href="http://www.baidu.com" @click="getTarget($event)" data-index="{{index}}">{{$index}}{{item.text}}<br></a> <input type="text" id="attr"> </body> <script> new Vue({ el:'body', data:{ items:[ {text:'小黄'}, {text:'小明'}, {text:'小红'}, {text:'小蓝'} ] }, methods:{ getTarget:function (ev) { ev.preventDefault();//禁止跳转 let target = event.target;//取当前点击目标 console.log(target.getAttribute('data-index'));//取当前点击目标的属性 document.getElementById('attr').value=target.getAttribute('data-index'); } } }) </script> </html>