简体   繁体   English

如何将 javascript 中的元素应用于 html 中的多个 class 名称

[英]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.

相关问题 如何解析html元素的类名? - How do I parse class names of an html element? 如何将 Javascript 函数应用于一个类的多个实例? - How do I apply Javascript functions to multiple instances of a class? 如何编辑多个类名? - How do I edit multiple class names? 如何使用JavaScript将类应用于元素,以使CSS转换有效? - How do I apply a class to an element with JavaScript in order for CSS transitions to work? 使用 javascript,如何从元素中的某些 class 名称中跳过或删除 e.stopPropagation()? - Using javascript, how do you skip, or remove, e.stopPropagation() from certain class names in an element? 如何使用id从javascript访问HTML元素,该html元素属于另一个html文档 - how do I access a HTML element from javascript using id,the html element belongs to another html document 如何将类或样式应用于IE中的某些HTML元素? - How could I apply the class or the style to some HTML element in IE? 如何在不指定顺序名称的情况下通过 Javascript 将目录中的图像添加到 HTML 元素 - How can I add images from a directory via Javascript to an HTML element without specifying sequential names 在Vanilla JavaScript中,如何显示/隐藏<span>多个类名?</span> - In Vanilla JavaScript, how do I show/hide <span>s with multiple class names? 如何在JavaScript中应用来自多个div元素的过滤 - How to apply filtering from multiple div element in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM