vue基础学习(二)
02-01 vue事件深入-传参、冒泡、默认事件
<div id="box">
<div @click="show2()">
<input type="button" value="按钮" @contextmenu.prevent="show($event)">
</div>
</div>
<script>
window.onload= function(){
//事件:@contextmenu右键点击触发、@click左键点击触发 //事件简写:v-on:click等价于@click
//事件传参:fn($event,XX) //$event表示触发的事件对象
//阻止事件冒泡:@click.stop="fn()" //等价于js方法ev.cancelBubble=true;
//默认事件:@contextmenu.prevent="fn()" //等价于js方法:ev.preventDefault(); new Vue({
el:"#box",//选择器
data:{
'msg':'welcome vue'
},
methods:{
show:function(ev){
//alert(ev.clientX);
alert(1);
//ev.cancelBubble=true;//js方法:阻止冒泡;vue中的简写@click.stop="show($event)"
//ev.preventDefault();//js方法:阻止默认事件;vue中简写@contextmenu.prevent="fn()"
},
show2:function(){
alert(2);
}
}
});
}
</script>
02-02 vue事件深入-键盘事件及按键编码事件
<div id="box">
<input type="text" @keyup.enter="show($event)"> </div>
<script>
//事件:@keydown="fn()"、@keydown="fn()" //按键编码:@keyup.13="fn()"或 @keyup.enter/up/right/bottom/left等 ; 等价于js中:ev.keyCode window.onload= function(){
new Vue({
el:"#box",//选择器
data:{ },
methods:{
show:function(ev){
//alert(ev.keyCode);//获取按键的编码
//if(ev.keyCode==13){//应用-识别按键回车;等价于@keyup.13="show()"
alert("您按了回车键")
//}
}
}
});
}
</script>
02-03 vue中操作基础属性与特殊属性class
<style>
.color_red{color:red;}
.bg_blue{background-color:blue;}
.font12{font-size:12px;}
</style>
<div id="box">
<img :src="url" :width="w" :title="altText" alt="this is a pic"><br>
<strong :class="[red,b]" class="font12">彩色文字(数据变量控制)</strong><br><br>
<strong :class="{color_red:boole_t,bg_blue:true}">彩色文字(类控制)</strong><br><br>
<strong :class="json">彩色文字(json数据控制)</strong>
</div>
<script>
//属性操作:
//结构:v-bind:属性=“属性值变量”;v-bind可以省略,如: :src="url" ;width="w"
//特殊属性:class和style
//:class="[red,b] red是数据
//:class="{color_red:boole_t,bg_blue:true}" color_red是类,boole_t可以是布尔值或数据
//:class="json" json是以个json数据 window.onload= function(){
new Vue({
el:"#box",//选择器
data:{
'url':'https://b-ssl.duitang.com/uploads/blog/201406/30/20140630102145_xSBWt.jpeg',
'w':'200px',
"altText":'这是一个图片',
'red':'color_red',//值表示的是类
'b':"bg_blue",//值表示的是类
'boole_t':'true',//布尔值变量
'boole_f':'false',
'json':{color_red:true,bg_blue:true},
},
methods:{ }
});
}
</script>
02-04 vue中操作特殊属性style
<div id="box">
<strong :style="[c,b]">彩色文字(数据变量控制)</strong><br><br>
<strong :style="{color:varColor,backgroundColor:'blue'}">彩色文字(样式配合数据)</strong><br><br>
<strong :style="json">彩色文字(json数据)</strong><br><br>
</div>
<script>
//特殊属性:class和style
//:style="[c,b]" c和b是数据
//:style="{color:varColor,backgroundColor:'blue'}" color是样式名,varColor可以是变量或值
//:style="json" json是以个json数据
//注意:复合样式一定要采用驼峰命名法 如:backgroundColor window.onload= function(){
new Vue({
el:"#box",//选择器
data:{
'varColor':'red',
'c':{color:'red'},
'b':{backgroundColor:'blue'},
'json':{color:'red',backgroundColor:'blue'}
},
methods:{ }
});
}
</script>
02-05 vue的模板{{msg}}、{{*msg}}、{{{msg}}} 的区别
<div id="box">
<input type="text" v-model="msg" /><br>
直接表示变量:{{msg}} == <span v-text="msg"></span><br>
只能控制一次:{{*msg}}<br>
直接表示变量,可以识别代码:{{{msg}}} == <span v-html="msg"></span>
</div>
<script>
//特殊属性:
//{{msg}} 直接表示变量,不会识别代码 等价于属性:v-text = "msg"
//{{*msg}} 只能控制一次,后来的不会同步
//{{{msg}}} 会识别代码 等价于属性:v-html = "msg"
window.onload= function(){
new Vue({
el:"#box",//选择器
data:{
'msg':'abc<strong>标签内</strong>',
},
methods:{ }
});
}
</script>
02-06 vue的过滤器,如:uppercase、lowercase、capitalize、curreoy
<div id="box">
小写转大写:{{msg|uppercase}} <br><br>
小写转大写:{{'welcome'|uppercase}} <br><br>
大写转小写:{{'WELCOME'|lowercase}} <br><br>
大写转小写再首字母大写:{{'WELCOME'|lowercase | capitalize}} <br><br>
美元:{{12 | currency}} <br><br>
元:{{12 | currency '¥'}} <br><br>
</div>
<script>
//过滤器:过滤模板的数据
//语法:{{msg | filterA | filterB }}
// uppercase 转大写 eg: {{msg|uppercase}}
// lowercase 转小写 eg: {{'WELCOME'|lowercase}}
// capitalize 首字母转大写 eg: {{msg|capitalize}}
// currecy 钱 eg: 美元:{{12 | currency}} 元:{{12 | currency '¥'}} window.onload= function(){
new Vue({
el:"#box",//选择器
data:{
'msg':'abc',
a:'english',
},
methods:{ }
});
}
</script>
02-07 vue的交互GET获取一个普通文本数据及给服务器发送数据
<script>
//交互:$http
// get:
// 获取一个普通文本数据
// this.$http.get('data/aa.txt').then(function(res){
// alert(res.data);
// },function(res){
// alert(res.status);
// })
//
// 给服务发送数据
// get: function () {
// this.$http.get('data/get.php', {
// a: 1,
// b: 2
// }).then(function (res) {
// //成功后执行
// alert(res.data);
// }, function (res) {
// //失败时执行
// alert(res.status);
// });
//} window.onload = function () {
new Vue({
el: 'body',
data: { },
methods: {
get: function () {
this.$http.get('data/get.php', {
a: 1,
b: 2
}).then(function (res) {
//成功后执行
alert(res.data);
}, function (res) {
//失败时执行
alert(res.status);
});
}
}
});
};
</script>
02-08 vue的交互POST向服务器发送数据并返回
<script>
//交互:$http
// post
// 给服务发送数据
// this.$http.post('data/post.php',{
// a:1,
// b:2
// },{
// emulateJSON:true
// }).then(function(res){
// alert(res.data);
// },function(res){
// alert(res.status);
// }) window.onload= function(){
new Vue({
el:"#box",//选择器
data:{ },
methods: {
// post 给服务器发送数据
get:function(){
this.$http.post('data/post.php',{
a:1,
b:20
},{
emulateJSON:true//不能少,post必须有这个标志,模拟json数据
}).then(function(res){
alert(res.data);
},function(res){
alert(res.status);
})
}
}
});
}
</script>
02-09 vue中交互jsonp的跨域请求
<h1>vue交互必须引入vue-resouce</h1>
<div id="box">
<button type="button" @click="get()">按钮</button>
</div>
<script>
//交互:$http
// jsonp 跨域请求
window.onload= function(){
new Vue({
el:"#box",//选择器
data:{ },
methods: {
// jsonp 跨域请求
get:function(){
this.$http.jsonp('https://sug.so.360.cn/suggest', {
word : 'a'
}).then(function(res){
alert(res.data.s);
},function(res){
alert(res.status);
})
}
}
});
} // jsonp 跨域请求 ,有时需要声明修改callback的名称
//window.onload= function(){
// new Vue({
// el:"#box",//选择器
// data:{ // },
// methods: {
// // jsonp 跨域请求
// get:function(){
// this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
// wd : 'b'
// }, {
// jsonp: 'cb' //callback的名字,默认名字就是callback
// }).then(function (res) {
// alert(res.data.s);
// },function(res){
// alert(res.status);
// })
// }
// }
// });
//}
</script>
02-12 vue交互实例:百度搜索下拉
<style>
.gray{background-color:gray;}
</style>
<h1>百度搜索下拉案例</h1>
<div id="box">
<input v-model="t1" @keyup="get($event)" @keydown.down.prevent="changeDown()" @keydown.up.prevent="changeUp()" type="text" style="width:600px;height:35px;line-height:45px;font-size:16px;"/>
<ul>
<li v-for="value in myData" :class="{gray:$index==now}">
{{value}}
</li>
</ul>
<p v-show="myData.length==0">暂无数据</p>
</div>
<script>
//交互:$http
// jsonp 跨域请求 window.onload= function(){
new Vue({
el:"#box",//选择器
data:{
myData: [],
t1: '',
now :'-1'
},
methods: {
// jsonp 跨域请求
get: function (ev) {
if (ev.keyCode == 38 || ev.keyCode == 40) { return }; //判断按上下键时不请求数据 if (ev.keyCode == 13) { //判断当按下回车键,进行搜索数据并打开新网页
window.open('https://www.baidu.com/s?wd=' + this.t1);
this.t1 = ''; //清空输入框
} this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su', {
wd:this.t1 //数据参数
}, {
jsonp:'cb'
}).then(function (res) {
this.myData = res.data.s
}, function () {
alert('error')
})
},
changeDown: function () { //按下键向下选择
this.now++;
if (this.now == this.myData.length) { this.now = -1 };
this.t1 = this.myData[this.now]; //输入框数据同步
},
changeUp() { //按上键向上选择
this.now--;
if (this.now == -1) { this.now = this.myData.length };
this.t1 = this.myData[this.now];
} }
});
}
</script>
vue基础学习(二)的更多相关文章
- Vue – 基础学习(1):对生命周期和钩子函的理解
一.简介 先贴一下官网对生命周期/钩子函数的说明(先贴为敬):所有的生命周期钩子自动绑定 this 上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周 ...
- Python入门基础学习 二
Python入门基础学习 二 猜数字小游戏进阶版 修改建议: 猜错的时候程序可以给出提示,告诉用户猜测的数字偏大还是偏小: 没运行一次程序只能猜测一次,应该提供多次机会给用户猜测: 每次运行程序,答案 ...
- Vue – 基础学习(4):事件修饰符
Vue – 基础学习(3):事件修饰符
- Vue – 基础学习(3):$forceUpdate()和$nextTick()的区别
Vue – 基础学习(3):$forceUpdate()和$nextTick()的区别
- Vue – 基础学习(2):组件间 通信及参数传递
Vue – 基础学习(2):组件间 通信及参数传递
- Python基础学习二
Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...
- Go基础学习(二)
数组[array] 数组定义[定义后长度不可变] 12 symbol := [...]string{USD: "$", EUR: "€", GBP: " ...
- 前端Vue基础学习
Vue基础 对与vue的简洁明了的干货分享,适合小白学习观看,如果有笔误或者想了解更多的,请给笔者留言,后续会继续分享,从零开始的vue相关经验 1.创建vue实例 <div id=" ...
- Django基础学习二
今天继续学习django的基础 学习用户提交url如何获得返回值 1.首先需要在工程的urls文件定义指定的urls要路由给哪个函数 在这个例子中,我们定义home的urls路由给views里的tes ...
- vue组件学习(二)
父子组件之间的数据传递, 父传给子: 直接在组件上传,如:<count :number="2"> (冒号和不要冒号的区别,有冒号会自动转为相应的类型)在名为count的 ...
随机推荐
- web更改AD用户密码
web更改AD用户密码 #web更改AD密码 #网站配置 绑定域名ad.test.cn 功能,更改AD用户密码 #参考http://bbs.51cto.com/thread-1379675-1.htm ...
- go语言常用开源库整理
框架 https://github.com/go-martini/martini 图形验证码 https://github.com/dchest/captcha ORM https://github. ...
- PHP 算法
1.首先来画个菱形玩玩,很多人学C时在书上都画过,咱们用PHP画下,画了一半. 思路:多少行for一次,然后在里面空格和星号for一次. ? 1 2 3 4 5 6 <?php for($i=0 ...
- 【NOIP2013提高组】火柴排队
https://www.luogu.org/problem/show?pid=1966 Σ(ai-bi)2=Σai2+Σbi2-2Σai*bi,要使Σ(ai-bi)2最小,则需2Σai*bi最大. 由 ...
- 洛谷 P3928 Sequence
题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强.小强便打算找出这三个数列内的最长波动数列. ...
- 读取指定文件夹下的全部文件,可通过正则进行过滤,返回文件路径数组 -- 基于node的一个函数
var fs = require('fs'); // 模板文件夹路径 var templateDirectory = '../src'; //相对于当前文件的相对路径 //var templateDi ...
- Cocos2d-x 3.0 Android改动APK名、更改图标、改动屏幕方向、改动版本,一些须要注意的问题
非常多新手程序员做出一个游戏后,编译成apk安装在手机上.却发现安装程序名和游戏图标都是Cocos2dx默认的,并且默认屏幕方向是横向.那么须要怎么才干改动为自己想要的呢? 打开你创建的project ...
- 数据结构(C实现)------- 最小生成树之Prim算法
[本文是自己学习所做笔记.欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020] 算法描写叙述 假设连通图是一个网,则称该网中全部生成树中权值总和最小的生成树 ...
- Chromium与CEF的多进程模型及相关參数
CEF基于Chromium,也是多进程模型.关于进程模型.參考这里:https://www.chromium.org/developers/design-documents/process-model ...
- openstack-glance API 镜像管理的部分实现和样例
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免.欢迎指正. 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...