gpt4 book ai didi

javascript - 将 forEach 循环输出到 innerHTML

转载 作者:行者123 更新时间:2023-11-30 07:51:52 24 4
gpt4 key购买 nike

愚蠢的问题,适应使用 ES6/Vanilla JS。

这是与此处的 console.log 完美配合的循环....

const theServices = 
["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


theServices.forEach(function(element) {
let formName = element;
formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

console.log( `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`)
});
<div id="place-here"></div>

现在尝试使用 innerHTML 插入 DOM 返回未定义。输出这些循环的合适方法是什么?

const theServices = 
["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


let theseCheckBoxes =
theServices.forEach(function(element) {
let formName = element;
formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

return `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`
});
;

document.querySelector('#place_here').innerHTML = theseCheckBoxes;
<div id="place_here"></div>

最佳答案

ForEach 不返回任何内容。所以 theseCheckBoxes 将是未定义的。尝试将其更改为 map()join() 结果。

const theServices = 
["Accounting", "Big Data", "Business", "Category", "Concept", "Consultant", "Consumer", "Corporate", "Cost", "Customer", "Development", "Digital", "Distribution", "Due Diligence", "Financial", "Global Sourcing", "Go-to-market", "Growth", "Improvement", "Information", "Technology", "Innovation", "Lean", "Management", "Manufacturing", "Marketing", "Merchandising", "Mergers & Acquisitions", "Operations", "Organization / Organizational", "Performance", "Portfolio", "Post-merger", "Pricing", "Procurement", "Product", "Profitability", "Purchasing", "Restructuring", "Retail", "Revenue", "Sales", "Strategy", "Supply Chain", "Sustainable", "Technology", "Transformation", "Turnaround", "Zero-based", "Budgeting"];


let theseCheckBoxes =
theServices.map(function(element) { // <-- map instead of forEach
let formName = element;
formName = formName.replace(/[^A-Za-z0-9]/g, '_').toLowerCase();

return `
<div><input type="checkbox" id="${formName}" name="cpg_services" value="${formName}" />
<label for="${formName}">${element}
</label>
</div>
`
});
;

document.querySelector('#place_here').innerHTML = theseCheckBoxes.join('\n');
<div id="place_here"></div>

关于javascript - 将 forEach 循环输出到 innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51483027/

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