button.html文件
<!DOCTYPE html> <html> <head> <title>DOM</title> </head> <body> <button id="button">this is a button</button> </body> </html>
然后在开发者工具的控制台(console)中加入button对象的点击事件:
button.onclick = function(){ alert("Hello World"); }
点击按钮时的效果:
为什么会有一个button对象呢? 因为浏览器解析上面的HTML时,会把标记解析为对象,如将<button>元素解析为button对象, 因此我们会有一个button对象 那button对象的onclick属性是怎么来的呢? 在此网站(https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLButtonElement)中的 的HTMLButtonElement,HTMLElement, Element和Node接口中均没有找到onclick属性
吃鸡游戏
哔哔one
相关分类