Vue 入门例子

原文链接: Vue 入门例子

上一篇: vue 对象改变时,同步更新视图的办法

下一篇: Vue 入门

绑定class

根据条件展示不同的样式

会使当前点击的对象改变颜色

  • event.target返回触发事件的元素

  • event.currentTarget返回绑定事件的元素

条件显示 v-show

135823_t7Fp_2856757.png

重置后会根据原始数据改变样式

135856_ovN5_2856757.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/uikit.min.css">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <title>迷宫寻路</title>
    <script src="js/jquery-3.2.1.js"></script>
    <script src="js/popper.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/vue.js"></script>
    <script src="js/uikit.min.js"></script>
    <script src="js/maze.js"></script>
</head>
<body>
<div id='myDiv' class="container">
    <div>
        <h4>样式绑定</h4>
        <button v-for="(i,index) in items"
                style="margin-top: 100px;width: 60px;height: 60px"
                :posx='index'
                @click="btn_click($event)"
                v-bind:class="{'btn':true, 'btn-primary':(index === currentPage)}">
            {{index}}
        </button>
    </div>

    <div>
        <h4>条件显示</h4>
        <button v-for="(i,index) in states"
                class="btn"
                style="margin-top: 100px;width: 60px;height: 60px"
                :posx='index'
                v-show="i==true"
                @click="btn_click2($event)">
            {{index}}
        </button>
    </div>
    <button @click='reset' class="btn">重置</button>
</div>
<script>
    var myDiv = new Vue({
        el: "#myDiv",
        data: {
            currentPage: 0,
            items: new Array(10),
            states: [true, true, true, true, true, true, true, true, true, true]
        },
        methods: {
            btn_click: function (event) {
                var x = parseInt(event.currentTarget.getAttribute('posx'))
                console.log('click')
                console.log(event)
                this.currentPage = x
            },
            btn_click2: function (event) {
                var x = parseInt(event.currentTarget.getAttribute('posx'))
                console.log('click2')
                console.log(event)
                this.$set(this.states, x, false)
            },
            reset: function () {
                this.currentPage = 0

                //指向的对象改变了,不用另行通知
                this.states = [true, true, true, true, true, true, true, true, true, true]
            }
        }
    })

</script>
</body>
<script>


</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值