對于下面的代碼,我不想重復三遍,這比忽略 icona、iconb、icon 這樣的鍵要好。在另一篇文章中,如何從包含三個對象的數組中獲取每個對象?,我在一個數組中涉及三個對象,這里我只想創建一個包含相同三個節點元素的對象。var iconArray = { icona: document.createElement('div'), iconb: document.createElement('div'), iconc: document.createElement('div') }順便說一句,為了匹配兩個對象的索引來設置值,如該帖子中的以下代碼,因此如果“iconsData”對象包含三個數組,則“iconArray”對象包含三個節點元素,或者可能都包含四個或五個元素。Object.keys(iconsData).forEach(function(value, indexa) { iconsData[value].forEach(function (obj,indexb) { Object.values(iconArray).forEach(function(keyc, indexc) { var img = document.createElement('img'); if(indexc === indexa){ img.addEventListener('mouseup', function () { keyword.beforePopup(obj.popup); }); img.setAttribute('style', '' + 'cursor:pointer!important;' + 'display:inline-block!important;' + ''); keyc.appendChild(img); } }); }); });
如何創建一個包含三個節點元素并設置相同值的對象?
拉風的咖菲貓
2023-11-12 14:45:01