首先贴上测试代码<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body><button id="botest" onclick="botest()">绑定</button><p/>
<button id="untest" onclick="untest()">取消绑定</button><p/>
<button id="test">click me</button>
<script language=Javascript>
var LPEvent = new Object();
LPEvent.addListener=function(o, evType, f, capture){
if(o.addEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.addEventListener(evType, f, capture);
}else if (o.attachEvent)
o.attachEvent("on" + evType, f);
else
eval("o.on" + evType + "=f;");
};
LPEvent.removeListener=function(o, evType, f, capture){
if(o.removeEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.removeEventListener(evType, f, capture);
}else if(o.detachEvent){
o.detachEvent("on" + evType, f);
}else{
eval("o.on" + evType + "=null;");
}
};
Function.prototype.bindAsEventListener = function(o) {
var _m = this;
return function(evt,r) {return _m.call(o, evt || window.event);}
};
function T(){
this.i=0;
this.test=function()
{
alert(this.i);
}
this.botest=function()
{
LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
this.untest=function()
{
LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
}
var t= new T();
function botest()
{
t.botest();
}
function untest()
{
t.untest();
}</script>
</body>
</html>通过这种方式绑定的事件监听方法 this.test.bindAsEventListener(this),无法取消绑定,即this.untest执行无效。盼高手提供解决办法,怎样才能取消绑定呢?
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body><button id="botest" onclick="botest()">绑定</button><p/>
<button id="untest" onclick="untest()">取消绑定</button><p/>
<button id="test">click me</button>
<script language=Javascript>
var LPEvent = new Object();
LPEvent.addListener=function(o, evType, f, capture){
if(o.addEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.addEventListener(evType, f, capture);
}else if (o.attachEvent)
o.attachEvent("on" + evType, f);
else
eval("o.on" + evType + "=f;");
};
LPEvent.removeListener=function(o, evType, f, capture){
if(o.removeEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.removeEventListener(evType, f, capture);
}else if(o.detachEvent){
o.detachEvent("on" + evType, f);
}else{
eval("o.on" + evType + "=null;");
}
};
Function.prototype.bindAsEventListener = function(o) {
var _m = this;
return function(evt,r) {return _m.call(o, evt || window.event);}
};
function T(){
this.i=0;
this.test=function()
{
alert(this.i);
}
this.botest=function()
{
LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
this.untest=function()
{
LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
}
var t= new T();
function botest()
{
t.botest();
}
function untest()
{
t.untest();
}</script>
</body>
</html>通过这种方式绑定的事件监听方法 this.test.bindAsEventListener(this),无法取消绑定,即this.untest执行无效。盼高手提供解决办法,怎样才能取消绑定呢?
解决方案 »
- json ajax
- firebug调试js问题
- 关于同一个页面调用不同的falsh的问题
- 怎样实现自动选择单选框并自动选择删除选项?
- js中td的文本对齐什么设置
- 请问firefox中怎么取得页面上iframe中的文本框?
- 如何删除父元素,保留子元素
- 在JSCRIPT中,怎样根据一个整数得到它对应的ASCII字符???
- 请问JAVASCRIPT的小问题,各位朋友请帮帮忙,很急!谢谢~~~
- ionic3使用导航时在url地址中会加上nav/n4,如何去除?
- ie8的原生json解析与序列化的问题
- 请教高手 $(".start").date_input is not a function O(∩_∩)O谢谢
<!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=gb2312" />
<body><button id="botest" onclick="botest()">绑定</button><p/>
<button id="untest" onclick="untest()">取消绑定</button><p/>
<button id="test">click me</button>
<script language=Javascript>
var LPEvent = new Object();
LPEvent.addListener=function(o, evType, f, capture){
if(o.addEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.addEventListener(evType, f, capture);
}else if (o.attachEvent){
o['e'+evType+f] = f;
o[evType+f] = function(){
o['e'+evType+f](window.event);
}
o.attachEvent('on'+evType,o[evType+f]);
}else
eval("o.on" + evType + "=f;");
};
LPEvent.removeListener=function(o, evType, f, capture){
if(o.removeEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.removeEventListener(evType, f, capture);
}else if(o.detachEvent){
o.detachEvent('on'+evType,o[evType+f]);
o[evType+f] = null;
}else{
eval("o.on" + evType + "=null;");
}
};
Function.prototype.bindAsEventListener = function(o) {
var _m = this;
return function(evt,r) {return _m.call(o, evt || window.event);}
};
function T(){
this.i=0;
this.test=function()
{
alert(this.i);
}
this.botest=function()
{
LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
this.untest=function()
{
LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
}
var t= new T();
function botest()
{
t.botest();
}
function untest()
{
t.untest();
}</script>
</body>
</html>
1、多次点击“botest”,即重复绑定多个相同函数,只能取消一个绑定的函数,其他的还是没取消掉
2、此方法对非IE浏览器无效再盼回复
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body><button id="botest" onclick="botest()">绑定</button><p/>
<button id="untest" onclick="untest()">取消绑定</button><p/>
<button id="test">click me</button><script language=Javascript>
var LPEvent = new Object();
LPEvent.addListener=function(o, evType, f, capture){
if(o.addEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.addEventListener(evType, f, capture);
}
else if (o.attachEvent) o.attachEvent("on" + evType, f);
else eval("o.on" + evType + "=f;");
if(!o.store) o.store = {};
if(!o.store[evType]){
o.store[evType]=[];
}
o.store[evType].push(f);
};
LPEvent.removeListener=function(o, evType, f, capture){
if(!o.store) o.store = {};
if(!o.store[evType]){
o.store[evType]=[];
}
if(o.store[evType].length==0) return;
var ff = o.store[evType].pop();
if(o.removeEventListener){
if(typeof(capture)=="undefined")
capture = false;
o.removeEventListener(evType, ff, capture);
}
else if(o.detachEvent) o.detachEvent("on" + evType, ff);
else eval("o.on" + evType + "=null;");
};Function.prototype.bindAsEventListener = function(o) {
var _m = this;
return function(evt,r){return _m.call(o, evt || window.event);}
};function T(){
this.i=0;
this.test=function()
{
alert(this.i);
}
this.botest=function()
{
LPEvent.addListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
this.untest=function()
{
LPEvent.removeListener(document.getElementById("test"),"click",this.test.bindAsEventListener(this),false);
}
}
var t = new T();
function botest()
{
t.botest();
}
function untest()
{
t.untest();
}</script>
</body>
</html>
function botest()
{
LPEvent.addListener(document.getElementById("test"),"click",function(){alert(0);},false);
}
function untest()
{
LPEvent.removeListener(document.getElementById("test"),"click",function(){alert(0);},false);
}