How to set focus on an element in Elm?

前端 未结 5 1512
暗喜
暗喜 2020-12-14 00:32

How can I set the focus on a Html element in Elm? I tried to set the autofocus attribute on the element and it only sets the focus on the page load.

5条回答
  •  别那么骄傲
    2020-12-14 00:42

    In Elm 0.19, use Browser.Dom.focus:

    import Browser.Dom as Dom
    import Task
    
    type Msg
        = NoOp
    
    focusSearchBox : Cmd Msg
    focusSearchBox =
        Task.attempt (\_ -> NoOp) (Dom.focus "search-box")
    

    You can choose to ignore if focusing fails like above or do something by triggering an update message.

提交回复
热议问题