gpt4 book ai didi

javascript - 如何通过 JavaScript 为每个字符串创建
  • 标签
  • 转载 作者:行者123 更新时间:2023-12-01 01:34:21 25 4
    gpt4 key购买 nike

    在 JavaScript 中我有:

    var cards = [
    'fa fa-diamond', 'fa fa-paper-plane-o', 'fa fa-anchor', 'fa fa-bolt',
    'fa fa-cube', 'fa fa-anchor', 'fa fa-leaf', 'fa fa-bicycle',
    'fa fa-diamond', 'fa fa-bomb', 'fa fa-leaf', 'fa fa-bomb', 'fa fa-bolt',
    'fa fa-bicycle', 'fa fa-paper-plane-o', 'fa fa-cube'
    ];

    我想为 ul 中具有类名 deck 的每个元素创建一个 li 标记。

    我怎样才能实现这个目标?

    var cards = ['fa fa-diamond', 'fa fa-paper-plane-o', 'fa fa-anchor', 'fa fa-bolt', 'fa fa-cube', 'fa fa-anchor', 'fa fa-leaf', 'fa fa-bicycle', 'fa fa-diamond', 'fa fa-bomb', 'fa fa-leaf', 'fa fa-bomb', 'fa fa-bolt', 'fa fa-bicycle', 'fa fa-paper-plane-o', 'fa fa-cube'];
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Matching Game</title>
    <meta name="description" content="">
    <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
    <link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Coda">
    <link rel="stylesheet" href="css/app.css">
    </head>
    <body>

    <div class="container">
    <header>
    <h1>Matching Game</h1>
    </header>

    <section class="score-panel">
    <ul class="stars">
    <li><i class="fa fa-star"></i></li>
    <li><i class="fa fa-star"></i></li>
    <li><i class="fa fa-star"></i></li>
    </ul>

    <span class="moves">3</span> Moves

    <div class="restart">
    <i class="fa fa-repeat"></i>
    </div>
    </section>

    <ul class="deck">
    // HERE I WANT TO BE ADD a li tag for each string from var cards
    </ul>
    </div>

    <script src="js/app.js"></script>
    </body>
    </html>

    最佳答案

    你可以用foreach来做到这一点循环。

    cards.forEach(function(item) {
    document.getElementById('deck').insertAdjacentHTML('beforeend', '<li><i class="'+item+'"></i></li>');
    });

    这里我使用了 ID 而不是 CSS 类 deck,这让事情变得更容易!

    关于javascript - 如何通过 JavaScript 为每个字符串创建 <li> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968214/

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