亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從數組創建無序列表的 DOM 函數

從數組創建無序列表的 DOM 函數

炎炎設計 2022-10-21 17:28:46
我嘗試創建一個從數組創建無序列表的 DOM 函數。例如,如果您將數組 ["hello", "food", "sun"] 傳遞給它,它將創建無序列表:<ul><li>hello</li><li>food</li><li>sun</li></ul>然而,它什么也沒創造。這是我的 DOM 函數的代碼:<script>function create_list(array,id){var ul= document.createElement("ul")ul.setAttribute("id",id)//sets the id of the ul tag to the id specified as argument.for (var i=0 ; i<array.length ; i++){ul.appendChild.document.createElement("li").textContent= array[i]//creates list elements inside of the ul tag.}document.body.appendChild(ul)//adds the ul tag to the body of the html document.}//call the functioncreate_list(["hello","13","Kitchen"],13)</script>為什么它不工作,我怎樣才能讓它工作?
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

您應該分開創建孩子和附加孩子的邏輯。


你的錯誤來自ul.appendChild.document.createElement..


我認為最好像下面這樣使用=)


function create_list(array, id) {

  var ul = document.createElement("ul")

  ul.setAttribute("id", id)

  //sets the id of the ul tag to the id specified as argument.

  for (var i = 0; i < array.length; i++) {

    var li = document.createElement("li");

    li.textContent = array[i]

    ul.appendChild(li);

    //creates list elements inside of the ul tag.

  }

  document.body.appendChild(ul)

  //adds the ul tag to the body of the html document.

}

//call the function

create_list(["hello", "13", "Kitchen"], 13)


查看完整回答
反對 回復 2022-10-21
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號