亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對象 arry 值到樣式元素

對象 arry 值到樣式元素

嗶嗶one 2021-06-01 13:12:38
我正在使用此代碼來獲取選中的復選框并將其名稱與對象數組匹配,以便能夠檢索某些值。樣品小提琴。如何使用這些值創建 span 元素并根據選中的復選框更新顏色設置位置?      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 => (          var marker = document.createElement('span');          marker.style.color = colCode:${item[chk.value].colCode}, //does not work          marker.style.marginLeft = pos:${item[chk.value].pos}px ) //does not work      );    <input type="checkbox" name="fruits" value="lychee">Lychee <br>     <input type="checkbox" name="fruits" value="orange" checked>Orange <br>    <input type="checkbox" name="fruits" value="apple">Apple <br>    <input type="checkbox" name="fruits" value="mango" checked>Mango <br>    <input type="checkbox" name="fruits" value="banana">Banana
查看完整描述

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>


查看完整回答
反對 回復 2021-06-03
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號