簡體   English   中英

從數組中導出無序列表

[英]derive unordered lists from an array

我有一系列句子,我希望轉換為無序的HTML列表,每個列表包含單個句子的單詞,例如[我彈鋼琴就可以'] t

<ul>
<li> id = number</li>
<li>I</li>
<li>play</li>
<li>piano</li>
<li>the</li>
<li>can</li>
</ul> 

我正在使用以下內容(我希望!!)遍歷數組以獲取我想要的格式

  function makeQuest() { var quest=['I play piano the can', 'tired I am', 'are seven There week in a days']; for (var i=0; i< quest.length; i++){ document.write('<ul class ="div3">') document.write('<li id = "number">' + (i + 1) + '.' + ' '+ '</li>') for (var j=0; j < quest[i].length; j++){ document.write('<li>') document.write(quest[i][j]) document.write('</li>' + '</ul>') } } }; makeQuest() 

相反,我使用這個腳本:

1.I
play piano the can
2. t
ired I am
3. a
re seven There week in a days.

我做錯了什么?

在空格上split字符串(你的方法是字符而不是單詞):

 function makeQuest() { var quest=['I play piano the can', 'tired I am', 'are seven There week in a days']; for (var i=0; i< quest.length; i++){ document.write('<ul class ="div3">') document.write('<li>' + (i + 1) + '. </li>') for (var j=0; j < quest[i].split(' ').length; j++){ document.write('<li>') document.write(quest[i].split(' ')[j]) document.write('</li>') } document.write('</ul>') } }; makeQuest() 

並且(這超出了您的問題范圍)不要id="number"使用id="number"

更多關於split()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM