vue小练习——选项卡切换

上午做了个选项卡的小练习,很简单,可以熟悉语法吧。
上代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
    body{
        font-family:"Microsoft YaHei";
    }
    #tab{
        width: 450px;
        height: 280px;
        margin: 0 auto;
        border: 1px solid cornflowerblue;
    }
    .tab-tit{
        font-size: 0;
        width: 450px;
        height: 40px;
    }
    .tab-tit a{
    	float: left;
        display: inline-block;
        height: 40px;
        line-height: 40px;
        font-size: 16px;
        width: 33.33%;
        text-align: center;
        background: #ccc;
        color: #333;
        text-decoration: none;
    }
    .tab-tit .cur{
        background: #09f;
        color: #fff;
    }
    .tab-con{
    	width: 100%;
    	text-align: center;
    }
    .tab-con span{
    	display: block;
    	margin-top: 100px;
    }
</style>
<body>
<div id="tab" >
    <div class="tab-tit" >
        <a href="javascript:;" v-for="(item,index) in tit" @click="skip(index)" :class="{'cur':item.curId===1}">{{item.title}}</a>
    </div>
    <div class="tab-con">
          <span v-text="span_data"></span>
    </div>
    </div>
</div>
</body>
<script>
  var vu=new Vue({
        el: '#tab',
        data: {
        	tit:[
	    {
            curId: 1,
            title:'html'
        },
         {
            curId: 0,
            title:'css'
        },
         {
            curId: 0,
            title:'javascript'
        },
	    ],
	    span_data:'html'
        },
        methods: {
        	skip:function(index){
        		for(var i=0;i<this.tit.length;i++){
        		this.tit[i].curId=0;
        		}
        		this.tit[index].curId=1;
        		this.span_data=this.tit[index].title;
        	}
        },
      
    })
</script>
</html>

【总结】
1、对于v-for的循环体有了更清楚的了解。
2、v-text={{}},而且v-text是单向绑定,也就是当vue里的对象值改变时页面显示的值会改变,但是页面值改变不会影响对象值。但是v-model可以实现双向绑定。v-model用于input,select等表单元素。其他地方使用不起作用。

猜你喜欢

转载自blog.csdn.net/qq_34448522/article/details/84645989