How can I dynamically create an unordered list in JavaScript?

前端 未结 4 2043
梦谈多话
梦谈多话 2021-01-12 00:28

I have a global JavaScript array that contains some strings.

I want to create a dynamic list based on the strings in my JavaScript array. Similar to this:

         


        
4条回答
  •  别那么骄傲
    2021-01-12 00:47

    Yes you can. You can either user the DOM directly with createElement (see Jonathan's answer), or go an very easy route with jQuery (not tested):

    var ul = "
      "; for(var i=0; i < arr.length; i++) { ul += "
    • " + arr[i] + "
    • "; } ul += "
    "; var ulElem = $(ul);

提交回复
热议问题