vue实现点击当前的li,显示当前的详情并且关闭其他的

 1 <template>
 2   <div id="appBox">
 3     <ul>
 4       <li v-for="(item,index) in itemList" :key='item.index' @click="showDetail(index, item.id)"> {{item.name}}
 5         <div class="detail" v-show="isShow === index">我是{{item.name}}</div>
 6       </li>
 7     </ul>
 8   </div>
 9 
10 </template>
11 <script>
12 export default {
13 
14   data () {
15     return {
16       isShow: 0, //这里默认为0,取到itemList数组里面第一条数据
17       itemList: [
18         { id: '1', name: '王源'},
19         { id: '2', name: '易烊千玺'},
20         { id: '3', name: '王俊凯'}
21       ]
22     }
23   },
24   methods: {
25     showDetail: function (index, id) {
26       let that = this;
27       if (that.isShow === index) {
28         that.isShow = -1;  //在这里就将它赋值为-1  由于:-1 !=  index 所以其他内容隐藏 ,被点击的则打开
29       } else {
30         that.isShow = index;   //这里是把index赋值给isShow,isShow=index则显示
31       }
32     }
33   }
34 }
35 </script>
36 
37 <style type="text/css">
38 * {
39   list-style: none;
40 }
41 #appBox{
42   width: 500px;
43   height: 500px;
44   margin:200px auto;
45 }
46 li{
47   margin-top: 10px;
48   cursor: pointer;
49 }
50 .detail {
51   border: 2px solid skyblue;
52   height: 50px;
53   margin-top: 10px;
54   width: 350px;
55 }
56 
57 </style>

 

posted @ 2019-06-24 09:36  全村最靓的程序猿  阅读(2051)  评论(0编辑  收藏  举报