本文介绍了JavaScript按钮onclick无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<button onclick="hello()">Hello</button>
<script>
function hello() {
alert('Hello');
}
</script>
这是我的代码.但这不起作用.当我单击按钮时,什么也没发生.
This is my code. But it's not working. When I click on the button nothing happens.
推荐答案
如何?
<button id="hellobutton">Hello</button>
<script>
function hello() {
alert('Hello');
}
document.getElementById("hellobutton").addEventListener("click", hello);
</script>
P.S.您应该在按钮上方放置hello().
P.S. You should place hello() above of the button.
这篇关于JavaScript按钮onclick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!