2017-06-23 32 views
0

如何從按鈕獲取標題?如何從html元素中檢索文本?

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

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

button沒有一個名爲text場。 - 類型的button 是:

Html Home.Msg 

不包含名爲text場。

注意,我意識到「按鈕」實際上是Html類型的消息。

回答

3

你需要把你的想法放在首位。而不是看按鈕文本中的內容,您需要將文本設置爲與設置類相同的階段。所以這就給你類似

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] 
3

如果不借助涉及端口和JavaScript的黑客攻擊,您無法從按鈕中獲取文本。此外,你不能真正檢查榆樹內的Elm虛擬DOM的任何內容。

相反,嘗試重構您的應用程序,以便您可以從模型中獲取信息。