2017-08-27 53 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