简体   繁体   中英

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". I'm trying to get h2 to display the word 'Accusation'. 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"

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM