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属性
2 回答

哔哔one
TA贡献1854条经验 获得超8个赞
HTML中的 <button>
标签实现自DOM的 HTMLButtonElement接口类
,
HTMLButtonElement又继承了一系列类,继承的基类是这个 EventTarget
。
最后的这个EventTarget是拥有 “通过属性注册事件” 的能力的
添加回答
举报
0/150
提交
取消