jqueryのinnerHTML等の一覧

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jqueryのinnerHTML等の一覧</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. <script type="text/javascript">
  9.   $(document).ready(function () {
  10.     ko.applyBindings(new ViewModel());
  11.   });
  12.   function ViewModel()
  13.   {
  14.     var self = this;
  15.     
  16.     sample = $('.sample');
  17.     
  18.     koView = "";
  19.     koView += "<table>";
  20.     koView += "<tr><th>入力内容</th><th>出力結果(タブや改行もちゃんと取得される)</th></tr>";
  21.     koView += ConvertHtml(sample[0].textContent, "textContent");
  22.     koView += ConvertHtml(sample[0].innerHTML, "innerHTML");
  23.     koView += ConvertHtml(sample[0].innerText, "innerText");
  24.     koView += ConvertHtml(sample[0].outerHTML, "outerHTML");
  25.     koView += ConvertHtml(sample[0].outerText, "outerText");
  26.     koView += "</table>";
  27.     
  28.     self.viewSource = ko.observable(koView);
  29.   };
  30.   
  31.   // 全置換:全ての文字列 org を dest に置き換える
  32.   function replaceAll(expression, org, dest)
  33.   {
  34.     return expression.split(org).join(dest);
  35.   }
  36.   
  37.   // 出力用にタグとか文字の置換え
  38.   function ConvertChar(original)
  39.   {
  40.     data01 = original;
  41.     data02 = replaceAll(data01, "<", "&lt;");
  42.     
  43.     lastData = data02;
  44.     return lastData;
  45.   }
  46.   
  47.   // 出力用にタグとか文字の置換え
  48.   function ConvertHtml(result, property)
  49.   {
  50.     data = "";
  51.     data += "<tr>";
  52.     data += "<th>sample[0]." + property + "</th>";
  53.     if(result == undefined)
  54.     {
  55.       data += "<td style='color:red;'><pre>このブラウザだと「" + property + "」は「undefined」</pre></td>";
  56.     }
  57.     else
  58.     {
  59.       data += "<td><pre>" + ConvertChar(result) + "</pre></td>";
  60.     }
  61.     data += "</tr>";
  62.     return data;
  63.   }
  64.   
  65. </script>
  66. <style type="text/css">
  67. body,th,td
  68. {
  69.   font-size:10pt;
  70. }
  71. table{
  72.   border-collapse:collapse;
  73.   margin:0;
  74. }
  75. td, th{
  76.   border:1px solid #ccc;
  77.   padding:5px;
  78.   text-align:left;
  79. }
  80. th{
  81.   background:#e0e0e0;
  82. }
  83. pre{
  84.   margin:0;
  85.   padding:0;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <h1>jqueryのinnerHTML等の一覧</h1>
  91. <div>
  92.   <div class="sample">
  93.     <a href="#">サンプルリンク</a><br />
  94.     <img src="images/sample_flow1.gif" alt="サンプル画像" />
  95. <p>サンプル文章1</p>
  96.     <p>サンプル文章2</p>
  97.   </div>
  98. </div>
  99. <h2>以下、「class="sample"」のinnerHTML等の一覧</h1>
  100. <div data-bind="html: viewSource"></div>
  101. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  102. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  103. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  104. </div>
  105. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  106. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  107. </div>
  108. </body>
  109. </html>