【问题标题】:How to pass function as a parameter in script tag?如何在脚本标签中将函数作为参数传递?
【发布时间】:2023-02-03 15:01:42
【问题描述】:

我正在寻找一种将函数作为参数传递给脚本标记的方法。例如,要进行以下工作:

<script src="http://path/to/widget.js?param_a=1&amp;param_b=3" data-myfunc={myfunction()}></script>
<script>
myfunction(){
console.log("hello world")
}
</script>

然后从脚本中触发函数。

因为我们可以在属性中传递值并使用 getAttributes 进行捕获:ref

【问题讨论】:

    标签: javascript node.js reactjs


    【解决方案1】:

    是的,有办法!

    你可以删除“()”

    只需转动:

    <script src="http://path/to/widget.js?param_a=1&amp;param_b=3" data-myfunc={myfunction()}></script>
    

    进入:

    <script src="http://path/to/widget.js?param_a=1&amp;param_b=3" data-myfunc={myfunction}></script>
    

    结束!

    很高兴为您提供帮助!

    顺便说一下,如果您有兴趣,请也帮助我:

    The is my question

    【讨论】:

      【解决方案2】:

      尝试这个

      <script>
      // move function definition above and pass function ref - don't call that function
      myfunction(){
      console.log("hello world")
      }
      </script>
      
      <script src="http://path/to/widget.js?param_a=1&amp;param_b=3" data-myfunc={myfunction}></script>
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-29
        • 2019-11-06
        • 2022-01-22
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2017-09-25
        • 2019-04-21
        相关资源
        最近更新 更多