【问题标题】:How to add EventListener to an html link?如何将 EventListener 添加到 html 链接?
【发布时间】:2019-11-29 19:34:50
【问题描述】:

没有添加EventListener并且没有执行测试代码而不是标准html按钮<input type="button"></input>我在renderer.js中参考<a></a>:

//code here is executed
...
window.user_button = document.querySelector('#lbUserButton')
user_button.addEventListener('Click', ()=>{
    //code here is not executed
    event.preventDefault();
    ipc.send('event-name');        
})

HTML:

<a id="lbUserButton" href="#" class="someCssClass">
    button title
</a>

【问题讨论】:

  • 你在用电子吗

标签: javascript html hyperlink electron addeventlistener


【解决方案1】:

该事件未执行,因为您为“Click”添加了一个事件侦听器,而它应该是小写的“click”。下面的代码将起作用。

user_button.addEventListener('click', () => {
...
})

【讨论】:

    【解决方案2】:

    试试这个:

    var user_button = document.queryselector('#lbUserButton')
    
    
    

    我不知道你是否已经定义了 user_button,也许你忘记了。如果要定义它,为什么要在它前面加上window.?我不认为这是必要的。至少,我从来没有说过。

    【讨论】:

      猜你喜欢
      • 2020-11-22
      • 2014-01-18
      • 2011-03-03
      • 1970-01-01
      • 2022-11-02
      • 2021-10-09
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多