html按钮未调用其onclick js函数 [英] html button not calling its onclick js function

查看:522
本文介绍了html按钮未调用其onclick js函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个简单的警报不起作用?

Why does this simple alert not work?

HTML:

<button id="mybutton" type="button" onclick="add()">Add</button>

JS:

function add(){
alert("hello???");
}

https://jsfiddle.net/k86mg0aj/

推荐答案

该问题特定于JSFiddle.您需要将LOAD TYPE更改为No wrap - bottom of <body>.

The problem is specific to JSFiddle. You need to change the LOAD TYPE to No wrap - bottom of <body>.

使用onLoad时,该函数不会成为全局函数,因此您不能直接从HTML调用它.如果它是全局的(例如使用无包装时),则可以使用.

When using onLoad, the function won't become global one, so you can't invoke it directy from HTML. If it is global - like when using no-wrap - it works.

这篇关于html按钮未调用其onclick js函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆