一个简单的vue小例子

主要功能

点击某一项的时候其变为黄色,然后自动计算所有变为黄色项目的数字之和。

学习到的知识点

v-on绑定click事件来改变data属性中的某一项
v-bind动态为元素添加class=‘active’属性
v-for循环插入元素

难点

大括号嵌套多了,容易糊涂 。
注意forEach遍历的用法。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<style>
    *{
    margin:0;
    padding:0;
}
    div {
        width:400px;
        background: pink;
        margin:50px auto; /* make it in the center*/
        padding:40px 80px;/* 40 means top and bottom ,while 80 means left and right*/
        
    }
    body{
    font-family:Microsoft YaHei;
}
    span {
        float:right;
    }
    ul {
        font-size:40px;
    }
    li {
        list-style-type: none;
    }
    ul li {

        margin-top:5px;
        margin-right:35px;
        background:palegreen;
    }
ul li.active{
    background-color:yellow;
}
p {
    font-size:40px;
    margin:10px 35px;
    background: pink;
    
}

</style>
<div id="main" v-cloak>
    <ul>
        <li v-for="item in items" v-on:click="toggleActive(item)" v-bind:class="{'active':item.active}">
            <!--v-bind的意思是说如果item.active是true,那么li就有<class="active">这个属性,否则就没有。这个是根据data中的
                值来决定的。
                v-on 依据点击,点击了就调用toggleActive(item)函数,这个函数改变active的属性,如果active是false,
                就改成true,如果是true,就改成false。
    
            -->
            {{item.name}}<span>{{item.price}}</span></li>
    </ul>
    <p>Total:<span>{{total()}}</span></p>
    
</div>
<script>
    var demo=new Vue({
        el:'#main',
        data:{
            items:[{
                name:'baicai',
                price:200,
                active:true,
            },
            {
                name:'doujiao',
                price:300,
                active:false,
            },
            
            {name:'tudou',
            price:220,
            active:false,}]
        },//, is necessary
        methods:{
            toggleActive: function(i){
            i.active = !i.active;
        },
        // change active value, i是形参。
            total:function(){
                var total=0;
                this.items.forEach(function(s){
                    if (s.active){
                    total+=s.price;
                    }
                    
                })//forEach over at here
                return total;
                
            }//total over at here
            
            
        }//methods over at here
        })//vue over at here
        
    
    
</script>

实现简单的计算功能。

image.png
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,257评论 0赞 29
  • 1.安装 可以简单地在页面引入Vue.js作为独立版本,Vue即被注册为全局变量,可以在页面使用了。 如果希望搭建...
    Awey阅读 11,455评论 4赞 129
  • 我用什么留住你? 想给你日暮的海风、 凌晨的街道、 搁浅游轮上熔金的日辉。 我给你一个久久守望归帆者的宁静。
    晔兴阅读 236评论 0赞 6
  • 浓密的胡须储存时光 时光越长,胡须越长 时光在粉璧上沉默已久 胡须在秋风中枯萎变黄 文字紧紧抱在一起,编织箩筐 冬...
    关月楼阅读 419评论 10赞 16
  • 2005年元月9日 阴冷 星期日 孩子已出生两个多月了,但一直没记下过什么只言片语,今天终于有了一点空闲的时间...
    听雨聆声阅读 234评论 0赞 1

友情链接更多精彩内容