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>
添加回答
舉報
