[英]How do i apply an element from javascript to multiple class names in the html
I'm trying to apply h2.appendChild(h2Text) to all three elements with the class name "flip-card-front".我正在尝试将 h2.appendChild(h2Text) 应用于 class 名称为“flip-card-front”的所有三个元素。 I'm trying to get h2 to display the word 'Accusation'.我试图让 h2 显示“指责”这个词。 To make things easier i only added three cards in the html but for my project i'm trying to apply the h2 to 100 cards with the class name "flip-card-front"为了方便起见,我只在 html 中添加了三张卡,但对于我的项目,我尝试将 h2 应用到 100 张卡,名称为 class
window.addEventListener("DOMContentLoaded", function(){
var h2 = document.createElement("h2");
var h2Text = document.createTextNode("Accusation ");
h2.appendChild(h2Text);
console.log(h2);
document.body.appendChild(h2);
});
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<script src="app.js" ></script>
</head>
<body>
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<h2>Accusations </h2>
<img
src='https://avataaars.io/?avatarStyle=Circle&topType=LongHairStraight&accessoriesType=Blank&hairColor=BrownDark&facialHairType=Blank&clotheType=BlazerShirt&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' />
</div>
<div class="flip-card-back">
<h1>John Doe</h1>
<p>Architect & Engineer</p>
<p>We love that guy</p>
</div>
</div>
</div>
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<img
src='https://avataaars.io/?avatarStyle=Circle&topType=ShortHairTheCaesarSidePart&accessoriesType=Blank&hairColor=BrownDark&facialHairType=Blank&clotheType=BlazerShirt&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' />
</div>
<div class="flip-card-back">
<h1>John Doe</h1>
<p>Architect & Engineer</p>
<p>We love that guy</p>
</div>
</div>
</div>
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<img
src='https://avataaars.io/?avatarStyle=Circle&topType=ShortHairShortFlat&accessoriesType=Kurt&hairColor=BrownDark&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=ShirtVNeck&clotheColor=Blue03&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' />
</div>
<div class="flip-card-back">
<h1>John Doe</h1>
<p>Architect & Engineer</p>
<p>We love that guy</p>
</div>
</div>
</div>
const front = document.getElementsByClassName("flip-card-front");
let header = document.createElement("h2");
header.innerHTML = "Accusation";
front.append(header);
ADDED:添加:
const cards = document.getElementById("cards"); function addTitleToCards() { for (let i = 0; i < cards.children.length; i++) { const card = cards.children[i] let header = document.createElement("h2") header.innerHTML = "Accusations" card.prepend(header) } } addTitleToCards()
<,DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width: initial-scale=1"> <title>Cards</title> </head> <body> <div id="cards"> <div class="flip-card"> <img src='https.//avataaars?io/:avatarStyle=Circle&topType=LongHairStraight&accessoriesType=Blank&hairColor=BrownDark&facialHairType=Blank&clotheType=BlazerShirt&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' /> <h3>John Doe</h3> </div> <div class="flip-card"> <img src='https.//avataaars?io/:avatarStyle=Circle&topType=ShortHairTheCaesarSidePart&accessoriesType=Blank&hairColor=BrownDark&facialHairType=Blank&clotheType=BlazerShirt&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' /> <h3>John Doe</h3> </div> <div class="flip-card"> <img src='https.//avataaars?io/?avatarStyle=Circle&topType=ShortHairShortFlat&accessoriesType=Kurt&hairColor=BrownDark&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=ShirtVNeck&clotheColor=Blue03&eyeType=Default&eyebrowType=Default&mouthType=Default&skinColor=Light' /> <h3>John Doe</h3> </div> </div> <body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.