如何从 html 元素中检索文本?

How do I retrieve text from an html element?

如何从按钮获取标题?

decorateOn : String -> Html Msg -> Html Msg
decorateOn selectedCaption button =

    if button.text == selectedCaption then
        button [ class "selectedNavigationButton" ] []
    else
        button [ class "navigationButton" ] []

button does not have a field named text. - The type of button is:

Html Home.Msg

Which does not contain a field named text.

请注意,我发现 "button" 确实属于 Html Msg.

类型

如果不诉诸涉及端口和 JavaScript 的黑客攻击,您将无法从按钮中获取文本。此外,您无法从 Elm 内部真正检查有关 Elm Virtual DOM 的任何内容。

相反,请尝试重构您的应用,以便您可以从模型中获取信息。

你需要转变你的想法。您需要在设置 class 的同一阶段设置文本,而不是查看按钮文本中的内容。所以这会给你类似

的东西
decorateOn : String -> Html Msg -> Html Msg
decorateOn selectedCaption button =    
    if selectedCaption == "the selected value" then
        button [ class "selectedNavigationButton" ] [text selectedCaption ]
    else
        button [ class "navigationButton" ] [text selectedCaption]