plantain
V2EX  ›  问与答

使用 webpack.js 打包一个主 js 文件后,怎么向 html 暴露一个 function?

  •  
  •   plantain · Jul 6, 2015 · 6154 views
    This topic created in 4059 days ago, the information mentioned may be changed or developed.
    主要动机是,动态生成的按钮,想直接 onclick到那个function。
    目前一个不优雅的方案是,把这个function定义为全局的。
    有什么其它方案吗?或者通过模板生成的不定数目的动态元素,怎么绑定事件比较好?
    5 replies    2015-07-06 18:27:05 +08:00
    BOYPT
        1
    BOYPT  
       Jul 6, 2015   ❤️ 1
    你执行js的时候绑定了这个按钮的事件不就完了么,为什么要暴露。
    yangg
        2
    yangg  
       Jul 6, 2015   ❤️ 1
    $("#permanent_element").on('click', '.selector to dynamic element', function() {
    });
    plantain
        3
    plantain  
    OP
       Jul 6, 2015
    @BOYPT 是声明式地生成的,设置viewmodel后,view不会立即改变,这时候绑定的不是最新的view,难道要用settimeout?
    kokdemo
        4
    kokdemo  
       Jul 6, 2015   ❤️ 1
    ……你是想说动态生成的dom绑定不了事件是么?

    请用 事件委托
    plantain
        5
    plantain  
    OP
       Jul 6, 2015
    啊,搞定了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4000 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.