首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bs-webapi -如何在Dom.nodeList上循环?

bs-webapi -如何在Dom.nodeList上循环?
EN

Stack Overflow用户
提问于 2017-08-28 03:36:03
回答 1查看 293关注 0票数 2

下面的代码不起作用,因为sides是一个Dom.nodeList,而DomTokenList.forEach需要一个Dom.domTokenList

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-28 04:05:00

出自@anmonteiro@anmonteiro的原因不一致:

代码语言:javascript
复制
Js.Array.forEach Js.log (NodeList.toArray sides);

下面是如何为NodeList中的每个元素执行setAttribute的示例。注意,Element.ofNode可用于将Dom.node转换为option Dom.element

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45908622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档