Javascript 在for循环中附加几个相同的元素

Javascript 在for循环中附加几个相同的元素,javascript,arrays,dom,for-loop,Javascript,Arrays,Dom,For Loop,在特定页面上,此标记出现多次: <li class= "PRICE"> <label>Total Value:</label> <span>$2,581</span> </li> 总价值: $2,581 我想使用Javascript将其动态更改为: <li class= "PRICE"> <a href="mailto:example@example.com">Additional Savin

在特定页面上,此标记出现多次:

<li class= "PRICE">
<label>Total Value:</label>
<span>$2,581</span>
</li>
  • 总价值: $2,581
  • 我想使用Javascript将其动态更改为:

    <li class= "PRICE">
    <a href="mailto:example@example.com">Additional Savings</a>
    </li>
    
  • 虽然我可以这样做一次,但更改页面上此标记的所有实例时遇到问题,因为我不确定如何创建同一元素的多个实例:

    var bottomSavings = document.getElementsByClassName("PRICE");
    var newLink = document.createElement('a');
    newLink.href = "mailto:example@example.com?";
    newLink.innerHTML = "Additional Savings";
    for (var i = i=0;i<bottomSavings.length;i++) {
        bottomSavings[i].removeChild(bottomSavings[i].children[0]);
        bottomSavings[i].removeChild(bottomSavings[i].children[0]);
        bottomSavings[i].appendChild(newLink);
    };
    
    var bottomSavings=document.getElementsByClassName(“价格”);
    var newLink=document.createElement('a');
    newLink.href=“mailto:example@example.com?";
    newLink.innerHTML=“额外节省”;
    
    对于(var i=i=0;i您需要为每个项目创建一个
    newLink
    元素。最简单的方法是将创建锚元素的代码也移动到循环中:

    var bottomSavings = document.getElementsByClassName("PRICE");
    for (var i = i=0;i<bottomSavings.length;i++) {
        var newLink = document.createElement('a');
        newLink.href = "mailto:example@example.com?";
        newLink.innerHTML = "Additional Savings";
        bottomSavings[i].removeChild(bottomSavings[i].children[0]);
        bottomSavings[i].removeChild(bottomSavings[i].children[0]);
        bottomSavings[i].appendChild(newLink);
    };
    
    var bottomSavings=document.getElementsByClassName(“价格”);
    对于(var i=i=0;i