下面的代码不起作用,因为sides是一个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);发布于 2017-08-28 04:05:00
出自@anmonteiro@anmonteiro的原因不一致:
Js.Array.forEach Js.log (NodeList.toArray sides);下面是如何为NodeList中的每个元素执行setAttribute的示例。注意,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
https://stackoverflow.com/questions/45908622
复制相似问题