-1

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

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

4 回答 4

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');
于 2012-07-12T10:58:55.833 回答
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);
于 2012-07-12T11:01:43.237 回答
0

只需使用$.each()

var listOfWords = ['foo', 'bar', 'sigma'];
var $list       = $('<ul/>');
$.each(listOfWords, function () { $list.append('<li>'+this+'</li>'); });
于 2012-07-12T11:02:57.393 回答
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>
于 2012-07-12T11:03:23.207 回答