knockoutjs:条件によって好きなstyleを指定:The style binding

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:条件によって好きなstyleを指定:The style binding</title>
  6. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  7. <style type="text/css">
  8. .hoge{
  9.   color:red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>knockoutjs:条件によって好きなstyleを指定:The style binding</h1>
  15. <div data-bind="style: { color: currentProfit() < 0 ? 'red' : 'black' }">
  16.   Profit Information
  17. </div>
  18. <script type="text/javascript">
  19.   var viewModel = {
  20.     currentProfit: ko.observable(150000) // Positive value, so initially black
  21.   };
  22.   viewModel.currentProfit(-50); // Causes the DIV's contents to go red
  23.   ko.applyBindings(viewModel);
  24. </script>
  25. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  26. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  27. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  28. </div>
  29. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  30. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  31. </div>
  32. </body>
  33. </html>