我在下面的代码是一个HTML页面,其中包含一个标签,我想使用我的JavaScript多维数组,并获取每个名字和年龄,以填充< h3>另外有任何简单的方法来实现这一点任何建议将不胜感激!
function openCity(evt,cityName) {
var i,tabcontent,tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active","");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
var personArr = [];
var person = {firstName:"John",lastName:"Doe",age:21};
var person2 = {firstName:"Paul",lastName:"Logan",age:22};
var person3 = {firstName:"Sean",lastName:"Kim",age:32};
var person4 = {firstName:"Ken",lastName:"Chow",age:12};
personArr.push(person,person2,person3,person4);
console.log(personArr);
body {font-family: Arial;}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.line1{
display:inline-block;
}
Meta name="viewport" content="width=device-width,initial-scale=1">
最佳答案
无需移动HTML,但您可以使用一些类和/或id来使JS更具可读性.
原文链接:https://www.f2er.com/html/425645.html首先,你需要获得所有的h3.这可以通过使用类line1定位父div并获取所有子项来实现.
var parent = document.getElementsByClassName('line1')[0].children;
然后你可以循环这个数组和对象数组来填充所有的innerHTML.在这里小心,你正在添加innerHTML而不是改变它.
这个llop可以通过很多方式实现,我通过循环每2 h3标签并保留一个标记来标记要添加的人
var personFlag = 0;
for(var i = 0; i < parent.length; i=i+2){
parent[i].innerHTML += personArr[personFlag].firstName +' '+personArr[personFlag].lastName;
parent[i+1].innerHTML += personArr[personFlag].age;
personFlag++
}
希望这会有所帮助:>
function openCity(evt,person4);
console.log(personArr);
var parent = document.getElementsByClassName('line1')[0].children;
console.log(parent);
var personFlag = 0;
for(var i = 0; i < parent.length; i=i+2){
parent[i].innerHTML += personArr[personFlag].firstName +' '+personArr[personFlag].lastName;
parent[i+1].innerHTML += personArr[personFlag].age;
personFlag++
}
body {font-family: Arial;}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.line1{
display:inline-block;
}
Meta name="viewport" content="width=device-width,'People')" id="defaultOpen">People