【问题标题】: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&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&param_b=3" data-myfunc={myfunction()}></script>
进入:
<script src="http://path/to/widget.js?param_a=1&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&param_b=3" data-myfunc={myfunction}></script>