2

Is there a function in purescript-halogen to select an element by it's id, or do I need to custom role it (which would seem very strange to me).

I'm reading through the documentation on Pursuit, and I see a selectElement function in Util, but nowhere do I see something that lets me select by id.

I can use getElementById :: ElementId -> NonElementParentNode -> Eff () (Nullable Element) to get an Element, but I do not know how to turn this Element into an HTMLElement.

The type search feature in Pursuit is also lacking, so I apologize for this naive question.

4

1 回答 1

5

这不是您在使用使用虚拟 DOM 的库时通常应该做的事情,因为如果您通过 id 保存对元素的引用,它最终可能会过时并引用完全不同的元素,或者不再存在的元素附加到 DOM。

获取元素的方法是使用,本指南的本节中ref有一个使用它的示例。它的工作原理类似于事件处理程序,只要元素存在或被删除,就会在组件上引发查询。如果您使用更新组件状态中的引用的查询,您可以确保知道您始终拥有您想要的实际元素(或者如果它由于某种原因不存在)。Nothing

如果你真的想使用,getElementById那么它可以从purescript-dom. 它不是 Halogen 的一部分,因为 Halogen 不适用于通用 DOM 操作。提供这些实用功能只是为了更轻松地初始化 Halogen 应用程序。

于 2017-01-14T12:17:43.497 回答