<script type="text/javascript">function nnn(vv){
this.vv = vv; this.uu = function(){
alert(this.cc);
}; this.kk = function(){
document.getElementById(this.vv).addEventListener("click",(this.uu),false);
}
}var bb = new nnn('fffff');
var hh = new nnn('ddddd');bb.cc = "hahaha";
hh.cc = "heheheh";bb.kk();
hh.kk();<button id="fffff">click me1</button>
<button id="ddddd">click me2</button>想把事件绑定到按钮上,请教大家正确的写法,谢谢
this.vv = vv; this.uu = function(){
alert(this.cc);
}; this.kk = function(){
document.getElementById(this.vv).addEventListener("click",(this.uu),false);
}
}var bb = new nnn('fffff');
var hh = new nnn('ddddd');bb.cc = "hahaha";
hh.cc = "heheheh";bb.kk();
hh.kk();<button id="fffff">click me1</button>
<button id="ddddd">click me2</button>想把事件绑定到按钮上,请教大家正确的写法,谢谢
<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 nnn(vv){
this.vv = vv; this.uu = function(x){
alert(x.vv);
}; this.kk = function(){
var a=this;
document.getElementById(this.vv).addEventListener("click",function(){a.uu(a)},false);
}
}
window.onload=function(){
var bb = new nnn('fffff');
var hh = new nnn('ddddd');bb.cc = "hahaha";
hh.cc = "heheheh";bb.kk();
hh.kk();}
</script>
</head><body>
<button id="fffff">click me1</button>
<button id="ddddd">click me2</button>
</body>
</html> 大体这样试试
ie9以前的可能不支持 加上attachEvent试试