zoukankan      html  css  js  c++  java
  • Vue 自定义指令练习

    Vue.directive(id,definition)注册一个全局自定义指令,接收两个参数,指令ID以及定义对象

    取值:

     <div v-demo="{ color: 'white', text: 'hello!' }"></div>
    
    Vue.directive("demo", function (el, binding) {
            alert(binding.value.color);
            alert(binding.value.text);
        })

    实例:

    用自定义指令实现全选和取消全选
    <div id="app">
        <ul>
            <li v-for="item in list">
                <input type="checkbox" v-sel="item.checked" />  {{item.name}}
            </li>
        </ul>
        <br />
        {{list}}
        <br />
        <button v-on:click="clickall(true)">全选</button>
        <button v-on:click="clickall(false)">取消全选</button>
    
    </div>
    <script type="text/javascript">
        Vue.directive("sel", function (el, v) {
            if (v.value) {
                $(el).attr("checked", "checked");
            } else {
                $(el).removeAttr("checked");
            }
    
        });
        
        var vm = new Vue({
            el: "#app",
            data: {
                list: [{ name: "足球", checked: true }, { name: "篮球", checked: false }, { name: "乒乓球", checked: true }]
            },
            mounted:function() {
            }
            ,
            methods: {
                clickall: function (flag) {
                    if (flag) {
                        this.list.forEach(function (v, i) {
                            v.checked = true;
                        });
                    } else {
                        this.list.forEach(function (v, i) {
                            v.checked = false;
                        });
                    }
                }
            }
        });
    </script> 

    高级功能:

        <div v-pin:true.left.bottom="true"></div>
    
    
    
       Vue.directive("pin", function (el, binding) {
    
            var pinned = binding.value;//取引号中的值
            var warning = binding.arg;//取:后面的值
            var position = binding.modifiers;//取.后页的参数
    
            for (var key in position){
                if (position[key]) {
                    el.position[key] = '10px';
                }
            }
        });
  • 相关阅读:
    R中character和factor的as.integer的不同
    ggplot2练习
    R的plotmath
    Python数据科学手册(2) NumPy入门
    Python数据科学手册(1) IPython:超越Python
    ggplot2(6) 标度、坐标轴和图例
    R自带数据集
    ggplot2(5) 工具箱
    MATLAB神经网络(7) RBF网络的回归——非线性函数回归的实现
    ggplot2(4) 用图层构建图像
  • 原文地址:https://www.cnblogs.com/lunawzh/p/7512558.html
Copyright © 2011-2022 走看看