设为首页收藏本站 |天气与日历| 2025-07-02 星期三 01:11:00 乙巳(蛇)年 六月初八 丑时
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 158|回复: 0

自定义指令

[复制链接]

954

主题

954

帖子

3879

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3879
发表于 2022-6-1 13:53:04 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue-v2.6.10.js"></script>
</head>
<body>
    <div id="app">
        <p v-red>123</p>
        <div v-red>4343455</div>
    </div>
</body>
<script>
    // 全局自定义指令
    // Vue.directive('red',{
    //     inserted:function(el){
    //         el.style.background = 'red';
    //     }
    // })

    var app = new Vue({
        el:'#app',
        data:{
            
        },
        // 局部自定义指令
        directives:{
            red:{
                inserted:function(el){
                    el.style.color = 'green';
                }
            }
        }
    });

</script>
</html>


[C++] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue-v2.6.10.js"></script>
</head>
<body>
    <div id="app">
        <p v-color="msg">123</p>
        <!-- <input type="text" v-model="msg"> -->
    </div>
</body>
<script>
    // 全局自定义指令
    // Vue.directive('red',{
    //     inserted:function(el){
    //         el.style.background = 'red';
    //     }
    // })

        // 自定义指令的值需要现有数据才能使用

    var app = new Vue({
        el:'#app',
        data:{
            msg:'red'
        },
        // 局部自定义指令
        directives:{
            color:{
                inserted:function(el,data){
                    el.style.color = data.value;
                    // console.log(data);
                }
            }
        }
    });

</script>
</html>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-7-2 01:11 , Processed in 0.101143 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表