bs-webapi - 如何遍历Dom.nodeList?

时间:2017-08-27 19:36:03

标签: reason bucklescript bs-webapi

由于双方是Dom.nodeListDomTokenList.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);

1 个答案:

答案 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