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("Demo2"));
<h1>演示 KnockOut js表达式充当绑定值功能</h1>
<hr />
<ul>
<li class="Point">
2、<b class="PointTitle">理解<span class="KeyWord">表达式值</span>绑定</b><br />
<ul class="PointDesc">
<li>可以使用任意javascript表达式表示bool值</li>
</ul>
<div class="PointDemo" id="Demo2">
<div data-bind="visible: shouldShowMessage()==true">
这里绑定的是js表达式 shouldShowMessage()==true
</div>
<button data-bind="click:switchVisibility,text:desc"></button>
</div>
<!--下面写一些特殊的提示信息-->
<div class="PSFrame">
shouldShowMessage()==true只是个演示,实际上可以使用任意的表达式,比如xxx().length<6
</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: