学习KnockOut第二篇之Counter
第一步。先写一个简单的text绑定吧。先写VM,再写激活代码,最后写V,那样V才会有智能提示。此处不多讲,上文中有写到过。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function() {
this.numberOfClick = ko.observable();
};
ko.applyBindings(new CounterViewModel());
</script>
text绑定
效果将会是这样:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
<button data-bind ="click:registerClick">Can U click me?</ button>
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function() {
this.numberOfClick = ko.observable();
//this.registerClick = this.numberOfClick(this.numberOfClick() + 1);
this.registerClick = function() {
return this.numberOfClick( this.numberOfClick() + );
};
};
ko.applyBindings(new CounterViewModel());
</script>
点击一次按钮数字就增加一次可好。click绑定。
我点击六次按钮后将会是这样:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
<button data-bind ="click:registerClick,disable:tooManyClicks"> Can U click me?</button>
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function() {
this.numberOfClick = ko.observable();
this.registerClick = function() {
return this.numberOfClick( this.numberOfClick() + );
};
this.tooManyClicks = ko.computed( function () {
return this.numberOfClick() >= ;
}, this);
};
ko.applyBindings(new CounterViewModel());
</script>
让按钮不能用吧。disable绑定。
看一下效果图,按钮里面字体颜色暗得不是很明显,不过真是的不能点击了,当点击次数有5次后:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
<button data-bind ="click:registerClick,disable:tooManyClicks"> Can U click me?</button>
<div data-bind ="visible:tooManyClicks">
I think you are tired,U can have a seat now.
</div >
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function() {
this.numberOfClick = ko.observable();
this.registerClick = function() {
return this.numberOfClick( this.numberOfClick() + );
};
this.tooManyClicks = ko.computed( function () {
return this.numberOfClick() >= ;
}, this);
};
ko.applyBindings(new CounterViewModel());
</script>
来点友情提示。visible绑定。
那么当点击5次后友情提示友好的出现了:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
<button data-bind ="click:registerClick,disable:tooManyClicks"> Can U click me?</button>
<div data-bind ="visible:tooManyClicks">
I think you are tired,U can have a seat now.
<button data-bind="click:resetClick"> Reset</ button>
</div >
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function () {
this.numberOfClick = ko.observable();
this.registerClick = function () {
return this.numberOfClick( this.numberOfClick() + );
};
this.tooManyClicks = ko.computed( function () {
return this.numberOfClick() >= ;
}, this);
this.resetClick = function() {
return this.numberOfClick();
};
};
ko.applyBindings(new CounterViewModel());
</script>
重置按钮,再现一次click绑定
此处不太好给图,当点击Reset的时候它真的就回到第一个图那里去了,不信你可以试试,此处无图亦有真相。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title >Ando's Counter </title>
</head>
<body>
<div >You've clicked <span data-bind="text:numberOfClick"></span > times</div >
<button data-bind ="click:registerClick,disable:tooManyClicks">< span data-bind="text:buttonText"></span ></button>
<div data-bind ="visible:tooManyClicks">
I think you are tired,U can have a seat now.
<button data-bind="click:resetClick"> Reset</ button>
</div >
</body>
</html>
<script src="knockout-2.3.0.js"></ script>
<script type="text/javascript">
var CounterViewModel = function () {
this.numberOfClick = ko.observable();
this.buttonText = ko.observable( "Can U click me?");
this.registerClick = function () {
return this.numberOfClick( this.numberOfClick() + );
};
this.tooManyClicks = ko.computed( function () {
this.numberOfClick() >= ;
if ( this.numberOfClick() >= ) {
return this.buttonText( "U can not click me now");
}
}, this);
this.resetClick = function() {
this.numberOfClick();
this.buttonText( "Can U click me?");
};
};
ko.applyBindings(new CounterViewModel());
</script>
按钮里的字也请楼主动态绑定。
附图一张:
学习KnockOut第二篇之Counter的更多相关文章
- 学习KnockOut第一篇之Hello World
学习KnockOut第一篇之Hello World 笔者刚开始学习KnockOut.写的内容就相当于一个学习笔记.且在此处向官网致敬,比较喜欢他们家的Live Example版块,里面有jsFiddl ...
- RabbitMQ学习总结 第二篇:快速入门HelloWorld
目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...
- JavaWeb学习总结第二篇--第一个JavaWeb程序
JavaWeb学习总结第二篇—第一个JavaWeb程序 最近我在学院工作室学习并加入到研究生的项目中,在学长学姐的带领下,进入项目实践中,为该项目实现一个框架(用已有框架进行改写).于是我在这里记录下 ...
- 老老实实学习WCF[第二篇] 配置wcf
老老实实学WCF 第二篇 配置WCF 在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Col ...
- 跟初学者学习IbatisNet第二篇
在上一篇里面我们知道了什么是IbatisNet,并且知道了如何用IbatisNet进行简单的增删改查的操作,在这一篇文章里面我们主要介绍一下IbatisNet操作存储过程. 我们一般把存储过程分为两种 ...
- Egret入门学习日记 --- 第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容)
第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容) 既然选好了Egret,那我就要想想怎么学了. 开始第一步,先加个Q群先,这不,拿到了一本<E ...
- Python学习【第二篇】Python入门
Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...
- Android学习笔记(第二篇)View中的五大布局
PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本... 学习内容: 1.用户界面View中的五大布局... i.首先介绍一下view的概念 view是什么呢?我们已经知道一个Act ...
- python学习【第二篇】初识python
python的安装 windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机] ...
随机推荐
- Table of Contents - Ehcache
Ehcache 2.9.x API Developer Guide Key Classes and Methods Basic Caching Cache Usage Patterns Searchi ...
- HTML5_用语义化标记重新定义博客
HTML5文档的第一行便是文档类型声明,文档类型声明的作用有两个 一:验证器依据文档类型来判断采用何种验证规则去验证代码 二:文档类型声明能够强制IE6,IE7,IE8以“标准模式”渲染页面 1: & ...
- Every student in every school should have the opportunity to learn to code
“I think everybody in this country should learn how to program a computerbecause it teaches you how ...
- EL表达式隐含对象
EL表达式语言中定义了11个隐含对象,使用这些隐含对象可以很方便地获取web开发中的一些常见对象,并读取这些对象的数据. 语法:${隐式对象名称} :获得对象的引用 <%@ page lang ...
- Part 1 some difference from asp.net to asp.net mvc4
Part 1 some difference from asp.net to asp.net mvc4 In MVC URL's are mapped to controller Action Met ...
- (转)Android之常用功能方法大集合
这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...
- C#中ToString和Formate格式大全
C#中ToString格式大全 stringstr1 =); //result: 56,789.0 stringstr2 =); //result: 56,789.00 stringstr3 =); ...
- Cocos2d-x坐标系介绍
在图形图像和游戏应用开发中坐标系是非常重要的,我们在Android和iOS等平台应用开发的时候使用的二维坐标系它的原点是在左上角的.而在Cocos2d-x坐标系中它原点是在左下角的,而且Cocos2d ...
- iOS 中对各种视图的截屏以及分享
1.一个第三方的工具,主要是对表视图.滚动视图.视图的扩展,用法也很简单 image = [tableview screenshot]; 2.然后将截的图片分享出去,在分享的时候,因为多个地方用到了截 ...
- C语言知识总结(3)
数组 数组的特点: 只能存放一种类型的数据,比如int类型的数组.float类型的数组 里面存放的数据称为“元素” 初始化方式 ] = {, , }; ] = {,}; , , }; ] = {[]= ...