1 回答

TA贡献1846条经验 获得超7个赞
由于您没有提供所需的结果,您的问题很不清楚您的代码旨在完成什么,但我可以帮助您摆脱语法错误。
您的代码存在以下问题:
您没有正确使用箭头功能。如果你打算在其中使用多个语句,或者定义变量,你应该使用大括号,然后返回你想要的结果。
您没有正确使用模板文字。为了让它们工作,它们必须用反引号括起来。
var inputElements = document.getElementsByName("fruits");
const item = {
"lychee": { price: 10, pos: 80, colCode: "ff0000" },
"orange": { price: 12, pos: 60, colCode: "00ff00" },
"apple": { price: 8, pos: 40, colCode: "ff6600" },
"mango": { price: 12, pos: 60, colCode: "00ff00" },
"banana": { price: 4, pos: 80, colCode: "ff0000" }
};
let result = [...document.querySelectorAll("[name=fruits]:checked")].map(chk => {
/* Create a <span> element. */
var marker = document.createElement("span");
/* Set the properties using template literals. */
marker.style.color = `#${item[chk.value].colCode}`;
marker.style.marginLeft = `${item[chk.value].pos}px`;
/* Put some content into the <span>. */
marker.textContent= "Content";
/* Append the <span> into the wrapper. */
wrapper.appendChild(marker);
/* Return the <span> so that it's cached inside the results array. */
return marker;
});
<input type="checkbox" name="fruits" value="lychee" checked>Lychee <br>
<input type="checkbox" name="fruits" value="orange" >Orange <br>
<input type="checkbox" name="fruits" value="apple" checked>Apple <br>
<input type="checkbox" name="fruits" value="mango" checked>Mango <br>
<input type="checkbox" name="fruits" value="banana">Banana
<div id = "wrapper"></div>
添加回答
举报