是否可以使用Javascript将数组内容放入HTML类中



我正在学习Javascript,并试图将数组的内容放入一个html类中
我做了一个这样的数组:const terms = ["term1","term2"];

<figure id="8-12" class=" *Here the contents of the array* ">
<a href="workshopPages/reeks/index.php">
<img src="media/fotos/gallery/reeks.png" />
<figcaption>CKV reeks</figcaption>
</figure>

有没有办法将数组的内容放入类中?

任何帮助都将不胜感激,谢谢!

您可以使用:

const terms = ["term1", "term2"];
let element = document.getElementById('8-12');
element.classList.add(...terms);

您可以参考此文档:https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

是,通过使用.classList

const terms = ["term1","term2"];
var cl = document.querySelector('figure').classList;
cl.add.apply(cl, terms);

您也可以使用.classList.add(... terms)作为@alex197应答,如:

const terms = ["term1","term2"];
document.querySelector('figure').classList.add(... terms);