首页 > 解决方案 > 在 WASM 上添加 addEventListener("click", f)

问题描述

我能够添加一个带有 nonclick操作的按钮,如下所示:

    var f js.Func
    f = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
        defer f.Release()
        Window.Call("alert", "Hello World")
        // or
        // alert := Window.Get("alert")
        // alert.Invoke("Hello World")
        return nil
    })
    // f = function(){ alert("Hello World") };


    var Window = js.Global()
    // document = window.document
    document := Window.Get("document")
    // btn = document.createElement("button")
    btn := document.Call("createElement", "button")
    // btn.innerText = "Click me"
    btn.Set("innerText", "Click me")
    // btn.style.background='#000000';
    btn.Get("style").Call("setProperty", "background-color", "blue")
    // btn.onclick = f
    btn.Set("onclick", f)
    // document.body.appendChild(btn)
    document.Get("body").Call("appendChild", btn)

它工作得很好,但我尝试复制 JS 代码:

btn.addEventListener("click",function(event){  f });

作为:

btn.Call("addEventListener", "click", f )
// or
btn.Call("addEventListener").Set("click", f)

但是他们都没有工作,知道为什么吗?

标签: gowebassembly

解决方案


推荐阅读