<html>
<head>
<script type="text/javascript">
function test(){
var obj = document.getElementById("main");
var items = [ "click", "keypress" ];
for ( var i = 0; i < items.length; i++ ) {
(function(){
var item = items[i];
obj[ "on" + item ] = function() { // 拼凑添加属性
alert( "Thanks for your " + item );
};
})();
}
}
//window.οnlοad=test; 注册事件的方式2
</script>
</head>
<body οnlοad="test()"> //方式1
<button type="button" id=" main" >Click Me!</button>
</body>
</html>
<head>
<script type="text/javascript">
function test(){
var obj = document.getElementById("main");
var items = [ "click", "keypress" ];
for ( var i = 0; i < items.length; i++ ) {
(function(){
var item = items[i];
obj[ "on" + item ] = function() { // 拼凑添加属性
alert( "Thanks for your " + item );
};
})();
}
}
//window.οnlοad=test; 注册事件的方式2
</script>
</head>
<body οnlοad="test()"> //方式1
<button type="button" id=" main" >Click Me!</button>
</body>
</html>