<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.or ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>親測測試</title>
<script type="text/javascript">
function oclick(){
alert('你點擊了我');
}
</script>
</head>
<body>
<!--//方式一-->
<input onclick="alert('你點擊了我')" value="點我1" type="button">
<input onclick="oclick()" value="點我2" type="button">
<!--// 方式二-->
<input id="demo" type="button" value="點我3">
<script>
document.getElementById("demo").onclick=function(){
alert(this.getAttribute("type"));
}
</script>
<!--//方式三 綁定事件監聽函數
addEventListener()函數 /attachEvent()來綁定監聽事件;-->
<!--elementObject.addEventListener(eventName,handle,useCapture);-->
<script type="text/javascript">
function addEvent(obj,type,handle){
try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
obj.addEventListener(type,handle,false);
}catch(e){
try{ // IE8.0及其以下版本
obj.attachEvent('on' + type,handle);
}catch(e){ // 早期瀏覽器
obj['on' + type] = handle;
}
}
}
addEvent(document.getElementById("demo"),"click",myAlert);
function myAlert(){
alert('你點擊了我');
}
</script>
</body>
</html>