You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从JS数组提取icon类名并添加到HTML的a元素中

Quick Tips to Connect Your Array to DOM Elements

Hey there! Glad you’ve already managed to log each icon value and the corresponding <a> elements—you’re already making great progress 😊

Here are some targeted hints to move forward:

  • First, make sure you have a collection of all your <a> elements (like using document.querySelectorAll('ul li a')). Double-check that the length of this collection matches your list array (both should be 5 items).
  • Both for loops and Array.prototype.forEach() work perfectly here! If you go with forEach, you can use its second parameter (the index) to link the array item to the right <a> element. For a for loop, just use the loop index to access both the array object and the DOM element.
  • The key action you need is to add the icon class to each <a>. Look into the DOM element’s classList.add() method—it’s a clean way to add multiple classes at once. (Alternatively, you can use className, but classList is more flexible for adding/removing classes later.)
  • Don’t forget to set the aria-hidden="true" attribute on each <a> element. The setAttribute() method on DOM elements will let you do this easily.

Try focusing on linking each array index to the corresponding <a> element first, then applying those two properties one by one. You’ve got this!

内容的提问来源于stack exchange,提问作者domin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:26:04