2

以下将不起作用,因为边是 aDom.nodeList并且DomTokenList.forEach期望 a 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);
4

1 回答 1

2

由@anmonteiro 提供的原因不和谐解释:

Js.Array.forEach Js.log (NodeList.toArray sides);

这是一个如何setAttribute为 a 中的每个元素设置的示例NodeList。注意,Element.ofNode可用于将 a 转换Dom.nodeoption 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

于 2017-08-27T20:04:59.967 回答