我根据time example写了一个简单的程序, 以测试事件中有哪些数据。它将 JSON 解码为值,然后将其编码回 JSON,然后在 SVG 文本元素中显示它。我得到的唯一结果是 {"isTrusted":true}
。
为什么会这样?我如何获得事件的另一个数据?我正在使用 Firefox 49 和 online compiler :
import Html exposing (Html)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Svg.Events exposing(on)
import Json.Decode as Json
import Json.Encode exposing (encode)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model = String
init : (Model, Cmd Msg)
init =
("No event", Cmd.none)
-- UPDATE
type Msg
= Event String
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Event event ->
(event, Cmd.none)
subscriptions model = Sub.none
stringifyEvent: Json.Encode.Value -> Msg
stringifyEvent x =
Event (encode 2 x)
-- VIEW
view : Model -> Svg Msg
view model =
svg [ viewBox "0 0 300 300", width "300px", on "mousedown" (Json.map stringifyEvent Json.value) ] [
text_ [x "0", y "30"] [text model]
]
当我在控制台中尝试时
svgElement.addEventListener('click', function(e) {console.log(e)})
它适用于所有属性。
最佳答案
我不知道有什么方法可以实现您的目标。 但我可以给你一个答案,为什么它会像你描述的那样。
如果你查看源代码,你会发现 Elm 运行时使用
JSON.stringify()
用于将 Value
转换为 String
。
你猜怎么着...
svgElement.addEventListener('click', function(e) {console.log(JSON.stringify(e))})
会给你 {"isTrusted":true}
当你点击...
关于Elm 除了 isTrusted 在 JSON 事件中没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727407/