-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7.Visible.html
More file actions
39 lines (30 loc) · 1.04 KB
/
7.Visible.html
File metadata and controls
39 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="../lib/knockout/dist/knockout.js"></script>
</head>
<body>
<div data-bind="visible: shouldShowMessage">
You will see this message only when "shouldShowMessage" holds a true value.
</div>
<script type="text/javascript">
// Visible绑定通过绑定一个值来确定DOM元素显示或隐藏
var viewModel = {
shouldShowMessage: ko.observable(true) // Message initially visible
};
// Knockout调用applyBindings激活viewModel
// (即把viewModel和View中的声明式绑定data-bind接洽关系起来)
ko.applyBindings(viewModel);
// 定时修改
setTimeout(function(){
viewModel.shouldShowMessage(false); // ... now it's hidden
console.log(false);
},5000);
// 定时修改
setTimeout(function(){
viewModel.shouldShowMessage(true); // ... now it's visible again
console.log(true);
},10000);
</script>
</body>
</html>