2017-08-27 52 views
0

由于双方是Dom.nodeListDomTokenList.forEach预计Dom.domTokenList以下将无法正常工作。bs-webapi - 如何循环Dom.nodeList?

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

你应该尽量多一点耐心和避免这样的不必要的交叉发布。当你发布这个消息时,它已经在Discord上得到了答复。 – glennsl

回答

1

从原因不和谐,@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