如何获取单词列表并从中创建一个 html 表?
<script>
$('.wordcontainer').html('<ul><li></li></ul>');
var listofwords = {"mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"};
</script>
如何获取单词列表并从中创建一个 html 表?
<script>
$('.wordcontainer').html('<ul><li></li></ul>');
var listofwords = {"mat","cat","dog", "pit", "pot", "fog", "log", "pan", "can", "man"};
</script>
像这样的东西应该可以解决问题。
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');
也许将您的代码更改为:
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);
只需使用$.each()。
var listOfWords = ['foo', 'bar', 'sigma'];
var $list = $('<ul/>');
$.each(listOfWords, function () { $list.append('<li>'+this+'</li>'); });
伙计尝试自己动手。不是每次你都会得到帮助。
祝你好运
<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>