gpt4 book ai didi

javascript - 使用数组创建 Div Dom 元素

转载 作者:行者123 更新时间:2023-11-28 14:32:07 25 4
gpt4 key购买 nike

我下面的代码有一个多维数组,我使用这个数组来动态创建DOM元素来填充我的HTML Tab 现在的问题是我正在尝试使用我的 ['hobbies'] 数组将其附加到 tab 中的每个名称下方。

我使用了 foreach 循环来使每个 [hobbies] 数组值都有自己的 div 标签。但我不确定如何使用每个 div 将其附加到名称下方。任何建议将不胜感激。 enter image description here

var personArr = [];
var person = {["first-Name"]: "John", ["last-Name"]: "Doe", ["age"]: 21, ["hobbies"]:["football","swimming","dancing"],
["person-desc"]: "<br />Bacon ipsum dolor amet short loin doner tail meatloaf hamburger jerky <br />boudin picanha shankle turducken prosciutto cow kielbasa tenderloin."
};
var person2 = {
["first-Name"]: "Paul",
["last-Name"]: "Logan",
["age"]: 22,
["hobbies"]:["camping","sleeping","eating"],
["person-desc"]: "Bacon ipsum dolor amet short loin doner tail meatloaf hamburger jerky boudin picanha shankle turducken prosciutto cow kielbasa tenderloin. "
};
var person3 = {
["first-Name"]: "Sean",
["last-Name"]: "Kim",
["age"]: 32,
["hobbies"]:["running","jogging","climbing"],
["person-desc"]: "Bacon ipsum dolor amet short loin doner tail meatloaf hamburger jerky boudin picanha shankle turducken prosciutto cow kielbasa tenderloin."
};
var person4 = {
["first-Name"]: "Ken",
["last-Name"]: "Chow",
["age"]: 12,
["hobbies"]:["gyming","movies","tv"],
["person-desc"]: "Bacon ipsum dolor amet short loin doner tail meatloaf hamburger jerky boudin picanha shankle turducken prosciutto cow kielbasa tenderloin."
};

personArr.push(person, person2, person3, person4);
console.log(personArr);
var $parent = $('.line1').eq(0);



personArr.forEach((person, i) => {
var $name = $('<h4/>');
var $desc = $('<p/>');
var $hobbie = $('<div/>');


var hobbieArray = '';
$.each(person['hobbies'], function (index) {
hobbieArray += ('<div class="hobclass ">' + person['hobbies'][index] + '</div>');
});

$name.html(` ${person['first-Name']} ${person['last-Name']}`);
$desc.html(` ${person['person-desc']}`);
$parent.append($name).append($desc).append($hobbie);

});


//<---------------------------------------------- TAB FUNCTION ------------------------------------------------------->
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";
}
document.getElementById("defaultOpen").click();
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;
}
.size{
width:50%;
}
a.morelink {
text-decoration:none;
outline: none;
}
.morecontent span {
display: none;

}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</head>
<body>
<div class ="size">
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'People')" id="defaultOpen">People</button>
</div>

<div id="People" class="tabcontent">
<div class="line1">

</div>
</div>
</div>

</body>
</html>

最佳答案

您只需将 hobbieArray (这是一个 HTML 字符串,而不是数组)插入到 $hobbie 元素中。像在其他地方一样使用 .html

var hobbieHTML = '';
$.each(person['hobbies'], function (index) {
hobbieHTML += ('<div class="hobclass ">' + person['hobbies'][index] + '</div>');
});
$hobbie.html( hobbieHTML );

使用 JS 编写 HTML 有点笨拙,使用元素通常更容易(就像您已经使用 $name$desc 所做的那样)

$.each(person['hobbies'], function (index) {
var $h = $('<div/>').addClass('hobclass').text( person['hobbies'][index] );
$hobbie.append( $h );
});

定义对象键时,您永远不需要同时使用 []""。仅当按键包含特殊字符时才需要 ""。如果可以的话,通常最好在命名键时不使用特殊字符。

var person2 = {
firstName: "Paul",
lastName: "Logan",
age: 22,
hobbies: [ "camping", "sleeping", "eating" ],
description: "Bacon ipsum dolor amet short loin doner tail meatloaf hamburger jerky boudin picanha shankle turducken prosciutto cow kielbasa tenderloin. "
};

现在可以更轻松地使用点表示法访问对象的属性。您还可以使用 native Array.forEach 而不是 jQuery 的 $.each

person.hobbies.forEach( function ( hobby ) {
var $h = $('<div/>').addClass('hobclass').text( hobby );
$hobbie.append( $h );
})
$name.html(` ${ person.firstName } ${ person.lastName }`);
$desc.html(` ${ person.description }`);

关于javascript - 使用数组创建 Div Dom 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51039634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com