Example 2: eventバインディング:foreachやwith内でのマウスオーバー

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Example 2: eventバインディング:foreachやwith内でのマウスオーバー</title>
  6. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  7. </head>
  8. <body>
  9. <h1>Example 2: eventバインディング:foreachやwith内でのマウスオーバー</h1>
  10. <ul data-bind="foreach: places">
  11.   <li data-bind="text: $data, event: { mouseover: $parent.logMouseOver }"> </li>
  12. </ul>
  13. <p>You seem to be interested in: <span data-bind="text: lastInterest"> </span></p>
  14. <script type="text/javascript">
  15.   function MyViewModel() {
  16.     var self = this;
  17.     self.lastInterest = ko.observable();
  18.     self.places = ko.observableArray(['London', 'Paris', 'Tokyo']);
  19.     // The current item will be passed as the first parameter, so we know which place was hovered over
  20.     self.logMouseOver = function(place) {
  21.       self.lastInterest(place);
  22.     }
  23.   }
  24.   ko.applyBindings(new MyViewModel());
  25. </script>
  26. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  27. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  28. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  29. </div>
  30. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  31. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  32. </div>
  33. </body>
  34. </html>