由于双方是Dom.nodeList
而DomTokenList.forEach
期望Dom.domTokenList
,因此以下工作无效。
open Bs_webapi.Dom;
external length : Dom.nodeList => int = "" [@@bs.get];
let sides = Document.querySelectorAll "#carousel > figure" document;
DomTokenList.forEach (fun item _ => print_endline item) (sides);
答案 0 :(得分:2)
从理性争议中解释,@anmonteiro提供:
Js.Array.forEach Js.log (NodeList.toArray sides);
以下是setAttribute
中每个元素NodeList
的示例。请注意,Element.ofNode
可用于将Dom.node
转换为option Dom.element
。
open Bs_webapi.Dom;
external length : Dom.nodeList => int = "" [@@bs.get];
let sides = Document.querySelectorAll "#carousel > figure" document;
Js.Array.forEachi
(fun side index =>
switch (Element.ofNode side) {
| Some element =>
Element.setAttribute "style" "some style here" element
| None => ()
}
)
(NodeList.toArray sides)
https://bucklescript.github.io/bucklescript/api/Js_array.html#VALforEach