plantain
V2EX  ›  问与答

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

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

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