Why doesn't this work?
<button onclick = "function(){alert('Hello');}">press me</button>
while this does:
<button onclick = "alert('Hello');">press me</button>
They both work. The first one defines a function, but doesn't call it. The second one actually calls alert
.
If you're trying to define and call an anonymous function, try this:
<button onclick = "(function(){alert('Hello');})()">press me</button>
Because you're not calling the function--you're defining it.
I don't know why you would , but you could write this:
<button onclick="(function() { alert('Hello'); })()">press me</button>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.