3 回答
![?](http://img1.sycdn.imooc.com/533e4bd900011a1d02000200-100-100.jpg)
TA贡献1876条经验 获得超5个赞
您正在执行该功能
(function fun() {
}()); <-- this is executing it
当你定义一个函数时,你就拥有了这个函数
function fun() {
}
![?](http://img1.sycdn.imooc.com/54584dd900014f6c02200220-100-100.jpg)
TA贡献1842条经验 获得超12个赞
如前所述,
如果您愿意,您也可以使用类似 html 的内容执行函数本身
<input type="button" onclick="fun()" value="test">
javascript
fun=()=>{
alert("hey there");
}
或者您可以删除 onclick 并创建这样的事件侦听器
html
<input id="testBTN" type="button" value="test">
javascript
document.getElementById("testBTN").addEventListener("click", ()=>{alert("hey man")});
![?](http://img1.sycdn.imooc.com/5458689e000115c602200220-100-100.jpg)
TA贡献1816条经验 获得超6个赞
我认为这是您在 vanilla JS 中寻找的解决方案:
window.onload = function(e){
console.log("window.onload: " + new Date());
}
function fun() {
location.reload();
}
<a href="javascript:void(0);" target="_self" id="ek_reload" onclick="fun()">MyMenuItem</a>
添加回答
举报