大佬教程收集整理的这篇文章主要介绍了在选项卡中显示javascript对象,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在下面的代码是一个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;
}
首先,你需要获得所有的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;
}
以上是大佬教程为你收集整理的在选项卡中显示javascript对象全部内容,希望文章能够帮你解决在选项卡中显示javascript对象所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。