我想创建并打印20个商家的列表。
服务器仅支持HTML和JavaScript,没有jQuery插件。
我需要使用一个数组来添加至少20个带有最小编码的业务。拥有能够轻松添加更多20家企业的代码,将是很好的。
每个数组需要打印一个单独的项目符号列表/无序列表(“ ul”)
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Yellow Pages</h2>
<p id="businessID"></p>
<script>
// Business Object
Business = {
"businessName",
"businessType",
"businessNumber",
}
// Constructor function for Business Object
function business( businessName, businessType, businessNumber ) {
this.businessName = name,
this.businessType = type,
this.businessNumber = number,
}
// Create 3 Business Objects
var business1 = new business("businessName1", "businessType1", "businessNumber1");
var business2 = new business("businessName2", "businessType2", "businessNumber2");
var business3 = new business("businessName3", "businessType3", "businessNumber3");
// Arrays of Business Objects, (Hopefully)
var businessesArray = [
{"businessName1", "businessType1", "businessNumber1"},
{"businessName2", "businessType2", "businessNumber2"},
{"businessName3", "businessType3", "businessNumber3"},
];
// Create Bulleted List & Print
var text = "<ul>";
for(i=0; i< businesses.length; i++){
for(j=0; j< businesses[i].length; j++){
text += "<li>" + businesses[i][j] + "</li>";
}
text += "<BR>";
}
text += "</ul>";
document.getElementById("businessID").innerHTML = text;
</script>
</body>
</html>
在这里https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default尝试代码
对不起,我是新手。请提供更多详细信息:-)
首先感谢您对我的第一个问题的帮助! :-)
答案 0 :(得分:1)
要基于给定的结构打印数组,请使用两个循环(一个用于内部数组,另一个用于外部数组):
for (var i = 0; i < businesses.length; i++) {
var ul = $("<ul/>");
$('#list').append(ul);
console.log((businesses[i]));
for (var j = 0; j < businesses[i].length; j++) {
var li = $("<li/>");
li.text(businesses[i][j]);
ul.append(li);
}
}