HTML代码
<div class="col-xs-12"><ul id="tabName" class="nav nav-tabs" ></ul></div>
JS代码
$(function () {//动态添加li标签var ul = document.getElementById("tabName");for (let i =0 ; i < str.length; i++){var sname= str[i];var newLi = document.createElement("li");if(i == str.length-1){newLi.className = "active";}newLi.innerHTML = '<a data-toggle="tab" href="#home" οnclick="showDetails(this)">'+sname+'</a>';ul.append(newLi);}/*let li = $("#tabName").find("li");if (typeof(li) == "undefined"){return;}console.log("li=="+li);console.log("li.classNam2"+li[0].className);li[3].className = "active";*/})
如果觉得《JS动态添加li标签》对你有帮助,请点赞、收藏,并留下你的观点哦!