knockoutjs:Example 1: 値があれば表示、なければ非表示:The visible binding

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Example 1: 値があれば表示、なければ非表示:The visible binding</title>
  6. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  7. </head>
  8. <body>
  9. <h1>knockoutjs:Example 1: 値があれば表示、なければ非表示:The visible binding</h1>
  10. <div data-bind="visible: shouldShowMessage">
  11.   You will see this message only when "shouldShowMessage" holds a true value.
  12. </div>
  13. <script type="text/javascript">
  14.   var viewModel = {
  15.     shouldShowMessage: ko.observable(true) // Message initially visible
  16.   };
  17.   
  18.   // shouldShowMessage()に引数として「false」を渡せば非表示、「true」を渡せば表示。
  19.   viewModel.shouldShowMessage(false); // ... now it's hidden
  20.   viewModel.shouldShowMessage(true); // ... now it's visible again
  21.   
  22.   ko.applyBindings(viewModel);
  23. </script>
  24. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  25. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  26. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  27. </div>
  28. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  29. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  30. </div>
  31. </body>
  32. </html>