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

Elm 如何从html元素中检索文本?,elm,Elm,如何从按钮获取标题 decorateOn : String -> Html Msg -> Html Msg decorateOn selectedCaption button = if button.text == selectedCaption then button [ class "selectedNavigationButton" ] [] else button [ class "navigationButton" ] []

如何从按钮获取标题

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

    if button.text == selectedCaption then
        button [ class "selectedNavigationButton" ] []
    else
        button [ class "navigationButton" ] []
按钮
没有名为
text
的字段-
按钮的类型
是:

它不包含名为
text
的字段


注意,我意识到“按钮”实际上是Html Msg类型。

如果不借助涉及端口和JavaScript的黑客攻击,就无法从按钮获取文本。此外,您不能从Elm内部真正检查关于Elm虚拟DOM的任何内容


相反,试着重构你的应用程序,这样你就可以从你的模型中获取信息。

如果不借助涉及端口和JavaScript的黑客技术,你就无法从按钮中获取文本。此外,您不能从Elm内部真正检查关于Elm虚拟DOM的任何内容


相反,试着重构你的应用程序,这样你就可以从你的模型中获得信息。

你需要改变你的想法。您需要在设置类的同一阶段设置文本,而不是查看按钮文本中的内容。这就给了你类似的东西

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]

你需要改变你的想法。您需要在设置类的同一阶段设置文本,而不是查看按钮文本中的内容。这就给了你类似的东西

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]