当用户点击榆木0.18中的按钮时,有没有办法将文本从div复制到剪贴板?
我看过Clipboard.elm,但我不能让它编译并在elm 0.18中工作。那么有没有正式的工作方式在elm 0.18中做到这一点?
答案 0 :(得分:4)
如果目标浏览器支持它,那么你 可以通过端口来实现,例如:
榆树:
type Msg = Copy
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case Debug.log "msg" msg of
Copy -> (model, copy ())
port copy : () -> Cmd msg
-- VIEW
view : Model -> Html Msg
view model =
div []
[ Html.input [ id "copy" ] []
, Html.button [ onClick Copy ] [ text "copy" ]
]
的javascript:
const app = Elm.Main.fullscreen();
app.ports.copy.subscribe(() => {
console.log('copy');
document.querySelector('#copy').select();
document.execCommand('copy');
});