onclick not working in external js-file, but in inline

亡梦爱人 提交于 2019-12-06 16:47:35

Without seeing more of your code, I would assume that you are creating and binding the changeColor() function in a javascript file that is loaded in the <head> of your HTML.

If so, the element with id headline doesn't exist yet (the javascript file is being processed before the HTML has fully loaded), so you are trying to bind to a non-existent element.

If this is the case, either move your script include to the bottom of the <body> element , or wrap the binding in a window.onload function as seen in this jsFiddle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!