简体   繁体   English

获取 Dom_html.element 的孩子

[英]Get children of Dom_html.element

In js_of_ocaml, is it possible to get the child nodes of Dom_html.element ?在 js_of_ocaml 中,是否可以获取Dom_html.element的子节点?

I know that the class inherits Dom.node , and thus has the childNodes method.我知道该类继承了Dom.node ,因此具有childNodes方法。 But since it is a method from Dom.node , it returns values of Dom.node types.但由于它是来自Dom.node的方法,它返回Dom.node类型的值。 And I need those nodes to still be Dom_html.element , or else most methods will not be available.而且我需要这些节点仍然是Dom_html.element ,否则大多数方法将不可用。

Since downcasting is not possible in OCaml, I do not find any possible solution for this issue.由于在 OCaml 中无法进行向下转换,因此我找不到针对此问题的任何可能的解决方案。 Am I missing something or is this really impossible?我错过了什么还是真的不可能?

childNodes cant't be typed as a collection of Dom_html.element s because the nodes returned can, and are likely to, include nodes that are not element s, such as text nodes. childNodes不能被输入为Dom_html.element的集合,因为返回的节点可以并且很可能包含不是element的节点,例如文本节点。

The DOM standard defines a property children on Element which would only return the elements, but that still wouldn't get you to Dom_html.element . DOM 标准在Element上定义了一个属性children ,它只会返回元素,但这仍然不会让你进入Dom_html.element And unfortunately it also does not seem to be included in JSOO's Dom.element .不幸的是,它似乎也没有包含在 JSOO 的Dom.element

You can use the element function of Dom.CoerceTo to safely coerce Dom.node s to Dom.element s, but I don't think there is any generally reliable way to go from Dom.element to Dom_html.element , because the DOM is unfortunately too dynamically typed.您可以使用element的功能Dom.CoerceTo安全强制Dom.node s到Dom.element S,但我不认为有任何一般可靠的方法,从去Dom.elementDom_html.element ,因为DOM是不幸的是太动态类型。

You might have to check the tagName manually and (unsafely) cast it using Js.Unsafe.coerce .您可能需要手动检查tagName并(不安全地)使用Js.Unsafe.coerce进行Js.Unsafe.coerce

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM