let a = (function gigel() { alert("dd");})(); * this is executed immediatly as the page loads* <button onclick="a">Click me</button>* this is executed only when clicking the button * <button onclick="alert('dd')">Click me</button>
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
您正在调用事件(function gigel...)(),因为调用返回任何内容a都不是函数。也a没有被调用,你会想要以下内容:
let a = (function gigel() {
alert("IIFE");
});
* this is executed immediately as the page loads*
<button onclick="a()">Click me</button>
* this is executed only when clicking the button *
<button onclick="alert('dd')">Click me</button>
或者,如果您想立即调用它并将其存储在a:
let a = (function gigel() {
alert("IIFE");
return gigel;
})();
* this is executed immediately as the page loads*
<button onclick="a()">Click me</button>
* this is executed only when clicking the button *
<button onclick="alert('dd')">Click me</button>
添加回答
举报
0/150
提交
取消