<script type="text/javascript">
function gel(id) {return document.getElementById(id);}
window.onload = function () {
var btn = gel("txtName"); //获得按钮对象
//btn.onclick = doClick; //先为按钮对象 onclick事件 设置了一个方法 doClick
// btn.addEventListener("click", doClick, true);
// gel("divC").addEventListener("click", doClick2, true);
// gel("divB").addEventListener("click", doClick3, true);
addEvent(btn, "click", doClick);
addEvent(gel("divC"), ‘click‘, doClick2);
addEvent(gel("divB"), ‘click‘, doClick3);
}
function doClick() {
alert("1");
}
function doClick2() {
alert("doClick2");
}
function doClick3() {
alert("doClick3");
}
function addEvent(obj, eventName, callback) {
if (obj.addEventListener) {
obj.addEventListener(eventName, callback, false);
} else {
obj.attachEvent("on" + eventName, callback);
}
}
</script>
</head>
<body>
<div id="divB">
<div id="divC">
<input id="txtName" type="button" value="测试事件" /><!--onclick="doClick(event)"-->
</div>
</div>
</body>