推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
austinchou0126

请教一个 Chrome 插件开发的问题

  •  
  •   austinchou0126 · Sep 12, 2014 · 2992 views
    This topic created in 4286 days ago, the information mentioned may be changed or developed.
    Content Scripts,我定义了一个函数A(),函数A的功能为产生一个Alert。然后我在页面某处append了一个button(id为btn),然后使用document.getElementById('btn').addEventListener("click", A());来为这个button绑定一个事件,但是在我实际测试的时候发现,页面载入完毕后Alert就弹了出来,而且后续再点这个button却不会弹出Alert了。
    不知道问题出在哪里,请教各位~
    5 replies    2014-09-12 17:07:59 +08:00
    austinchou0126
        1
    austinchou0126  
    OP
       Sep 12, 2014
    而且我也试过document.getElementById('btn').onclick=A();也无效
    eric_zyh
        2
    eric_zyh  
       Sep 12, 2014
    document.getElementById('btn').onclick=A;

    document.getElementById('btn').addEventListener("click", A);
    austinchou0126
        3
    austinchou0126  
    OP
       Sep 12, 2014
    @eric_zyh 那假如我A函数有参数呢?还有就是不太明白为何后续点button时没有反映
    piglei
        4
    piglei  
       Sep 12, 2014 via iPhone   ❤️ 2
    - A()表达式是A函数的返回值。
    - 事件绑定的应该是函数。
    - 如果A方法接受参数应该在A函数里面返回另外一个函数。
    austinchou0126
        5
    austinchou0126  
    OP
       Sep 12, 2014
    @piglei 讲明白了,谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   956 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 18:32 · PVG 02:32 · LAX 11:32 · JFK 14:32
    ♥ Do have faith in what you're doing.