knockoutjs:Example 2: ボタンクリックで値を消していく:The click binding

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Example 2: ボタンクリックで値を消していく:The click binding</title>
  6. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  7. </head>
  8. <body>
  9. <h1>knockoutjs:Example 2: ボタンクリックで値を消していく:The click binding</h1>
  10. <ul data-bind="foreach: places">
  11.   <li>
  12.     <span data-bind="text: $data"></span>
  13.     <button data-bind="click: $parent.removePlace">Remove</button>
  14.   </li>
  15. </ul>
  16. <script type="text/javascript">
  17.   function MyViewModel() {
  18.     var self = this;
  19.     self.places = ko.observableArray(['London', 'Paris', 'Tokyo']);
  20.     // The current item will be passed as the first parameter, so we know which place to remove
  21.     self.removePlace = function(place) {
  22.       self.places.remove(place)
  23.     }
  24.   }
  25.   ko.applyBindings(new MyViewModel());
  26. </script>
  27. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  28. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  29. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  30. </div>
  31. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  32. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  33. </div>
  34. </body>
  35. </html>