12.1.VUE學習之-循環li,if判斷示例講解class中應用表達式


功能:

當點擊按鍵時,改變當前循環數組里的status里的值,

判斷staus里的當前的值來,切換顯示 刪除恢復 的按鈕

判斷staus里的當前的值來改變span標簽里的字體顏色樣式

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>vue</title>
    <link rel="stylesheet" href="">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- <script type="text/javascript" src="../js/vue.js"></script> -->
    <style type="text/css">
        .success{
            color: green;
        }
        .error{
            color:red;
        }
    </style>
</head>
<body>
<div id="vue">
    <!--循環app里的news-->
    <li v-for="v in news">
        <!--當前數組v里的status為ture是就用sucess樣式,否則用error里的樣式-->
        <span :class="v.status?'success':'error'">{{v.title}}</span>
        <!--點擊時把當着數組傳給changeStatus方法,並把要改變的值也傳去,讓其在方法里改變當前數組里的值-->
        <button @click="changeStatus(v,false)" v-if="v.status">刪除</button> <!--判斷有status時才顯示按鈕-->
        <button v-on:click="changeStatus(v,true)" v-if="!v.status">恢復</button>
    </li>

</div>
</body>
<script type="text/javascript">
    var app=new Vue({
        el:'#vue',
        methods:{
            changeStatus(item,status){

                console.log(item); //item代表傳過來循環的當前數組
                console.log(status); //要改變的狀態
                item.status=status; //把當前數組里的status重新賦值

            }
        },
        data:{
            news:[
                {title:'haha',status:true},
                {title:'hehe',status:false},
            ],
        },
    });
</script>
</html>

效果:


免責聲明!

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



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