How to change div background color on button click?

后端 未结 1 1185

I can change the background color Red, Blue, as well as Green, but when I click the Reset button I get an error in Chrome(browser):

btnReset is not a function,

相关标签:
1条回答
  • 2021-01-28 14:12

    Be careful when you're using id on the html tags, because it makes an automatic var declaration using the id. So, you're overriding the btnReset function with the btnReset pointing to the <input>.

    Solution 1: change the id="btnReset" or the function btnReset() name!

    Solution 2: move your <script> to the end of the document (just before the </body> end tag, so your function overrides the automatic declarated var.

    Additional info: http://www.2ality.com/2012/08/ids-are-global.html

    0 讨论(0)
提交回复
热议问题