JavaScript - 从脚本中的单词列表创建html表或网格

时间:2012-07-12 10:55:59

标签: javascript jquery

如何获取单词列表并从中创建html表?

 <script>
   $('.wordcontainer').html('<ul><li></li></ul>');
   var listofwords = {"mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"};
 </script>

4 个答案:

答案 0 :(得分:0)

这样的事情应该可以解决问题。

var listOfWords = ["mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"];
var innerTable = '';
for (var i = 0, len = listOfWords.length; i < len; i++) {
  innerTable += '<tr><td>'+listOfWords[i]+'</td></tr>';
}
$('<table>'+innerTable+'</table>').appendTo('body');

答案 1 :(得分:0)

可能会更改您的代码:

var listofwords = ["mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"];
var table = "<table>";
for (var i = 0; i < listOfWords.length; i++) {
   table += '<tr><td>'+listOfWords[i]+'</td></tr>';
}
table += "</table>";
$("body").append(table);

答案 2 :(得分:0)

只需使用$.each()

var listOfWords = ['foo', 'bar', 'sigma'];
var $list       = $('<ul/>');
$.each(listOfWords, function () { $list.append('<li>'+this+'</li>'); });

答案 3 :(得分:0)

老兄试着自己动手吧。不是每次都会得到帮助 最好的运气

<script>
       $('.wordcontainer').html('<ul><li></li></ul>');
       var listofwords = ["mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"];
       var stringTable = '';
      foreach(var word in listofwords)
      {
         stringTable += '<tr><td>'+word+'</td></tr>';
      }
    $("body").append('<table>'+stringTable+'</table>')
    });
     </script>