親カテ残して子カテの表示・非表示

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>親カテ残して子カテの表示・非表示</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  8. <style type="text/css">
  9. .w{
  10.   display:table;
  11.   width:100%;
  12.   margin-top:1em;
  13. }
  14. .t{
  15.   display:table-cell;
  16.   vertical-align:top;
  17. }
  18. .img{
  19.   display:table-cell;
  20.   width:50px;
  21.   text-align:center;
  22.   vertical-align:top;
  23. }
  24. .line{
  25.   display:table;
  26.   width:100%;
  27. }
  28. .detail{
  29.   pDesing-top:0.5em;
  30.   pDesing-left:1em;
  31. }
  32. .ttl{
  33.   display:table-cell;
  34.   vertical-align:top;
  35. }
  36. h2.ttl{
  37.   margin:0;
  38.   font-size:1em;
  39. }
  40. .total{
  41.   display:table-cell;
  42.   width:300px;
  43.   text-align:right;
  44.   vertical-align:top;
  45. }
  46. .detail .line{
  47.   margin-top:5px;
  48.   padding-bottom:5px;
  49.   border-bottom:1px dotted #ccc;
  50. }
  51. .detail .ttl{
  52.   padding-left:1em;
  53. }
  54. .img img:hover{
  55.   cursor:pointer;
  56.   filter:alpha(opacity=30);
  57.   -moz-opacity:0.30;
  58.   opacity:0.30;
  59. }
  60. </style>
  61. <script type="text/javascript">
  62.   $(document).ready(function () {
  63.     function viewModel() {
  64.       var self = this;
  65.       
  66.       self.dataAry = ko.observableArray([
  67.         {
  68.           pNam: '鳥精', 
  69.           pDes: '鳥肉とネギを串にさしたヤツ', 
  70.           chil: [
  71.             {cNa: "たれ", cPr: "120円"},
  72.             {cNa: "塩", cPr: "120円"},
  73.             {cNa: "ネギ味噌", cPr: "150円"}
  74.           ]
  75.         },
  76.         {
  77.           pNam: '豚精', 
  78.           pDes: '豚肉とネギを串にさしたヤツ', 
  79.           chil: [
  80.             {cNa: "たれ", cPr: "120円"},
  81.             {cNa: "塩", cPr: "120円"},
  82.             {cNa: "塩麹", cPr: "150円"}
  83.           ]
  84.         },
  85.         {
  86.           pNam: '丸ごとシマウマ', 
  87.           pDes: 'シマウマを丸ごと串に刺したヤツ', 
  88.           chil: [
  89.             {cNa: "たれ", cPr: "110円"},
  90.             {cNa: "塩", cPr: "110円"},
  91.             {cNa: "ネギ味噌", cPr: "140円"},
  92.             {cNa: "塩麹", cPr: "140円"},
  93.             {cNa: "柚子コショウ", cPr: "960円"}
  94.           ]
  95.         }
  96.       ]);
  97.       
  98.       // クリックしたときの処理
  99.       self.clickData = function(thisData, allObj)
  100.       {
  101.         var dom = allObj.target;
  102.         var ary = $(dom).parents(".w").find(".detail");
  103.         
  104.         // 表示・非表示切り替え
  105.         ary.slideToggle();
  106.       };
  107.     }
  108.     ko.applyBindings(new viewModel());
  109.   });
  110. </script>
  111. </head>
  112. <body>
  113. <h1>親カテ残して子カテの表示・非表示</h1>
  114. <p>右のお花の画像クリックで表示・非表示</p>
  115. <!-- ko foreach: dataAry -->
  116. <div class="w">
  117.   <div class="t">
  118.     <div class="line">
  119.       <h2 class="ttl" data-bind="text: pNam"></h2>
  120.       <div class="total" data-bind="text: pDes"></div>
  121.     </div>
  122.     <div class="detail" data-bind="foreach: chil">
  123.       <div class="line">
  124.         <div class="ttl" data-bind="text: cNa"></div>
  125.         <div class="total" data-bind="text: cPr"></div>
  126.       </div>
  127.     </div>
  128.   </div>
  129.   <div class="img"><img href="//tips.recatnap.info/sample/images/sample_flow1.gif" data-bind="click: $root.clickData"></div>
  130. </div>
  131. <!-- /ko -->
  132. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  133. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  134. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  135. </div>
  136. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  137. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  138. </div>
  139. </body>
  140. </html>