【问题标题】:Communication between JS module and html (in fullduplex)JS模块与html的通信(全双工)
【发布时间】:2017-11-24 17:27:14
【问题描述】:

我有一个使用项目中其他模块的内联模块脚本。 我需要使用 onclick 或其他所有工作从 html 调用模块函数..

<script type="module">
   import * as blabla from './somemodule.js';

   function myfunction(){
      console.log("Youhou!");
    }

   //Communication from script to html works fine
   document.getElementById('test').onclick = myfunction;
</script>
<button id="test">Test</button>
<button onclick="myfunction();">Test</button>//This dont work

这甚至可能吗?在谷歌上找不到任何东西,我是一个很好的谷歌用户。 非常感谢!

【问题讨论】:

    标签: javascript html module ecma


    【解决方案1】:
    onClick="myFunction()"
    

    jQuery('#clickable').click( myFunction() );
    

    还有 VanillaJS 解决方案...

    getElementById('clickable').addEventListener('click', myFunction() )
    

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多