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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM