var SimpleListModel = function (items) {
var self = this;
self.shouldShowMessage = ko.observable(true) //初始化可见
self.switchVisibility = function () {
self.shouldShowMessage(!self.shouldShowMessage());//每次点击按钮的时候,修改observbale的值
};
self.desc = ko.computed(function () {
return self.shouldShowMessage() ? "点击隐藏" : "点击显示";
});//按钮上的文本值
};
ko.applyBindings(new SimpleListModel(), document.getElementById("Demo1"));
<h1>演示 KnockOut Visible Binding 功能</h1>
<hr />
<ul>
<li class="Point">
1、<b class="PointTitle">理解<span class="KeyWord">visible</span>绑定</b><br />
<ul class="PointDesc">
<li>visible属性影响的是style中的display:none</li>
</ul>
<div class="PointDemo" id="Demo1">
<div data-bind="visible: shouldShowMessage">
当"shouldShowMessage" 变量的值为true的时候.你能看到这段内容,
</div>
<button data-bind="click:switchVisibility,text:desc"></button>
</div>
<!--下面写一些特殊的提示信息-->
<div class="PSFrame">
注意 会被解析为false的值:bool的false,数值0,null或者undefined
</div>
<hr />
</li>
</ul>
<hr />
/*Demo�ļ���ʽ*/
body
{
background-color: #e3e5d4;
}
/* ֪ʶ�� */
.Point
{
margin: 20px,0,0,0;
}
/* ֪ʶ����� */
.PointTitle
{
color:#0d8d0d;
}
/* �ؼ��� */
.KeyWord
{
color:Red;
}
/* ֪ʶ������ */
.PointDesc
{
font-family: Arial;
font-size: 15px;
color:#09acb8;
}
.PointDesc li
{
margin: 10px,0,0,0;
}
/* ֪ʶ��Demo��� */
.PointDemo
{
border-style:double;
border-color:#dcd559;
min-height:100px;
font-family: Corbel;
font-size: 20px;
color: #00174f;
}
/* ��Ҫ������ʾ����Ϣ��� */
.PSFrame
{
border-style: dashed;
border-color:#78e6ef;
font-family: Corbel;
font-size: 20px;
font-style:italic;
color: #b86c00;
}
/* ������ʾ */
.error {
color:Red;
}
/* ********************************************************** */
/*��Ҫǿ������ʽ*/
.Notice
{
font-weight: bold;
font-family:Bodoni MT Black;
color: #cd0707;
}
/* ********************************************************** */
/*�������ʽ*/
input[type="text"]
{
min-width:250px;
}
select
{
min-width:400px;
}
External resources loaded into this fiddle: