Skip to content

事件

事件可以是:按钮被点击了、鼠标移动到某个位置了、按下键盘按键了……

常见事件

onchange html 元素已被改变 onclick 鼠标单击 onblur 元素失去焦点 onfocus 元素获得焦点 onload 某个页面或元素完成加载 onsubmit 表单提交时 onkeydown 按下键盘某个键时 onmouseover 鼠标移到某元素上 onmouseout 鼠标从某元素移开

事件绑定

HTML 的 onclick 属性

html
<body>
	<input id="submit_2" onclick="gotobaidu()" type="submit" value="百度">
</body>

<script>
    function gotobaidu() {
        location.href = "https://www.baidu.com/";
    }
</script>

对象调用 onclick 属性指定函数

html
<body>
	<input id="submit_2" type="submit" value="百度">
</body>

<script>
    document.getElementById("submit_2").onclick = function () {
        location.href = "https://www.baidu.com/";
    }
</script>