- 例子1:
<form name="form">
<input type="button" name="b1" id='b1' value="text" />
<script language="javascript">
var b1 = document.getElementById('b1');
var $str = 'hello ';
if(b1.attachEvent){ //MSIE 方法
b1.attachEvent("onclick", function(){alertMsg($str)}); //为b1做触发事件
}else if(b1.addEventListener){ // W3C方法
b1.addEventListener('click',function(){ alertMsg($str)},false); }
function alertMsg(s){
alert(s);
}
</script>
</form>
- 例子2: