1. 同一事件多个处理程序
<div id="demo">111111111<div> <input type="button" value="changeColor" onclick="toRed(), toBlue()"/>
var demo; function toRed() { alert("change to red!!") demo = document.getElementById("demo"); demo.style.backgroundColor = "red"; } function toBlue() { alert("change to blue```````````````````"); demo = document.getElementById("demo"); demo.style.backgroundColor = "blue"; }
2. window的三个事件
- onload 页面加载完毕,通常用于做一些初始化的工作
- onbeforeunload 关闭页面之前
- onunload 关闭页面(注, 此时window.alert()无法使用)
<input type="text" id="txt"/>
var txt; // 页面加载完毕后,使文本框获取焦点 window.onload = function() { txt = document.getElementById("txt"); txt.focus(); } // 页面关闭之前, 或者刷新时触发 window.onbeforeunload = function() { window.alert("onbeforeunload"); } window.onunload = function() { // 无法测 }
3. 关闭右键菜单
- retrun false
<!-- 注: firefox中 oncontextmenu 需绑定在window 上 --> <body oncontextmenu="return closeMenu()"> </body>
/* "return false;" 的含义 当右击时, 会触发内置的 function oncontextmenu() { return closeMenu(); } */ function closeMenu() { return false; } window.oncontextmenu = closeMenu;
4. 简单计算器的实现