简体   繁体   中英

Can someone explain to me why this button doesn't work?

I can't seem to figure out what I'm doing wrong. I want to call a function using onclick but it doesn't work and I dont know why.

<input type="button" onclick="reset()" value="reset">

<script>
function reset() {
alert();
}
</script>

demo: https://jsfiddle.net/3mp0869s/1/

It works here : https://jsfiddle.net/lotusgodkk/3mp0869s/2/

I believe it was a jsfiddle issue. You need to include inline script function in the html section.

<input type="button" onclick="reset()" value="reset">
<script>
    function reset() {
      alert();
    }
</script>

you have chosen 'onload' option on jsFiddle, check this updated fiddle

All you need to do is chose 'wrap in the head' option.

Since if you wrap this in an even function scope, it won't have a global scope and it won't be available for the onclick() event attribute handler of the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM