请选择 进入手机版 | 继续访问电脑版
收起左侧

vue-点击事件

[复制链接]

552

主题

569

帖子

1万

积分

管理员

站长

Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20

积分
16427

灌水之王

QQ
发表于 2018-12-4 10:54:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
实例代码如下:
[HTML] 纯文本查看 复制代码
<!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">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="app">
        {{message}}
        <br>
        {{messageReverse}}
        <br><br>
        <input type="text" v-model="message">
    </div>

    <script>
        var vm = new Vue({
            el : '#app',
            data : {
                message : 'hello vue1'
            },
            computed :  {
                messageReverse : function(){
                    return this.message.split('').reverse().join('');
                }
            }
        }); 
    </script>

<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>


我们给input添加一个点击事件:
[HTML] 纯文本查看 复制代码
<input type="text" v-model="message" v-on:click="changeBgColor">


这个点击事件需要写到methods 里面:
新建代码如下:
[HTML] 纯文本查看 复制代码
<!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">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="app">
        {{message}}
        <br>
        {{messageReverse}}
        <br><br>
        <input type="text" v-model="message" v-on:click="changeBgColor">
    </div>

    <script>
        var vm = new Vue({
            el : '#app',
            data : {
                message : 'hello vue1'
            },
            computed :  {
                messageReverse : function(){
                    return this.message.split('').reverse().join('');
                }
            },
            methods : {
                changeBgColor : function(ev){
                    ev.target.style.background = 'red';
                }
            }
        }); 
    </script>

<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>


当点击input标签后,input标签得背景色救护变成红色:


QQ:2424004764
回复

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|客户端下载|先锋者IT论坛 ( 湘ICP备16007032号湘公网安备 43030202001052号

GMT+8, 2024-3-29 04:39 , Processed in 0.094162 second(s), 14 queries , File On.

Powered by Discuz! X3.3

© 2017 bbs.fologde.com. Template By Gstudio

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