先发我写的代码function addEvent(obj, type, fn){
if (obj) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
var funIe = function(){
fn.call(window.event.srcElement);
};
obj.attachEvent("on" + type, funIe);
} else{
obj["on" + type] = fn;
}
};
}var removeEvent = function(obj, type, fn){
if(obj){
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + type, fn);
} else {
obj["on" + type] = null;
}
}
};
var ip = document.createElement("input"),currentCell;
var td2input = function(event) {
event == null ? currentCell=window.event.srcElement : currentCell=event.target;
ip.value=currentCell.innerHTML;
currentCell.innerHTML = "";
ip.onblur = blurDo;
currentCell.appendChild(ip);
ip.focus();
removeEvent(currentCell, "dblclick", td2input);
};function blurDo(event) {
currentCell.innerHTML = ip.value;
if(event != null)
addEvent(currentCell, "dblclick", td2input);
}var i,j,tp = new Array();
var tdInput = document.getElementsByTagName("td");
for(i = 0, j = 0; i < tdInput.length; i++)
if(tdInput[i].className == "td2input")
tp[j++] = tdInput[i];
for(i = 0; i < tp.length; i ++)
addEvent(tp[i], "dblclick", td2input);
现在有问题是IE不能移除侦听事件,也就是说,在IE中双击了之后再双击会出问题,还有就是,我想扩展下td2input 函数,添加几个参数,但是那样做的话,连chrome和firfox都不能呢个移除事件侦听了,请问我要怎么做才能完美的removeEventListener掉事件。或者有没有什么方法,可以获取某个元素的所有侦听事件?
if (obj) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
var funIe = function(){
fn.call(window.event.srcElement);
};
obj.attachEvent("on" + type, funIe);
} else{
obj["on" + type] = fn;
}
};
}var removeEvent = function(obj, type, fn){
if(obj){
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + type, fn);
} else {
obj["on" + type] = null;
}
}
};
var ip = document.createElement("input"),currentCell;
var td2input = function(event) {
event == null ? currentCell=window.event.srcElement : currentCell=event.target;
ip.value=currentCell.innerHTML;
currentCell.innerHTML = "";
ip.onblur = blurDo;
currentCell.appendChild(ip);
ip.focus();
removeEvent(currentCell, "dblclick", td2input);
};function blurDo(event) {
currentCell.innerHTML = ip.value;
if(event != null)
addEvent(currentCell, "dblclick", td2input);
}var i,j,tp = new Array();
var tdInput = document.getElementsByTagName("td");
for(i = 0, j = 0; i < tdInput.length; i++)
if(tdInput[i].className == "td2input")
tp[j++] = tdInput[i];
for(i = 0; i < tp.length; i ++)
addEvent(tp[i], "dblclick", td2input);
现在有问题是IE不能移除侦听事件,也就是说,在IE中双击了之后再双击会出问题,还有就是,我想扩展下td2input 函数,添加几个参数,但是那样做的话,连chrome和firfox都不能呢个移除事件侦听了,请问我要怎么做才能完美的removeEventListener掉事件。或者有没有什么方法,可以获取某个元素的所有侦听事件?
function numTwolimit(obj, reg) {
var num = obj.value;
if (reg.test(num) == false) {
obj.value = "";
}
}function addEvent(obj, type, fn){
if (obj) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
var funIe = function(){
fn.call(window.event.srcElement);
};
obj.attachEvent("on" + type, funIe);
} else{
obj["on" + type] = fn;
}
};
}var removeEvent = function(obj, type, fn){
if(obj){
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + type, fn);
} else {
obj["on" + type] = null;
}
}
};var ip = document.createElement("input"),currentCell;
var td2input = function(obj, fn, fn2) {
var regex = /input/i;
if(regex.test(obj.innerHTML))
return;
ip.value = obj.innerHTML;
ip.size = 4;
obj.innerHTML = "";
ip.onblur = function() {blurDo(obj, fn2)};
if(fn != "" && fn != null)
ip.onkeyup = fn;//function() {numTwolimit(ip, /^[1-9]{1}[0-9]{0,}$/)};
obj.appendChild(ip);
ip.focus();
};function blurDo(obj, fn) {
if(ip.value == "" || ip.value == null) {
eval(fn);
} else {
obj.innerHTML = ip.value;
}
}var i,j,tp = new Array();
var tdInput = document.getElementsByTagName("td");
for(i = 0, j = 0; i < tdInput.length; i++)
if(tdInput[i].className == "td2input")
tp[j++] = tdInput[i];
for(i = 0; i < tp.length; i ++)
addEvent(tp[i], "dblclick", function(obj) {return function() {td2input(obj, function() {numTwolimit(ip, /^[1-9]{1}[0-9]{0,}$/)}, "alert(\"不能为空,请输入一个大于0的数\");ip.focus();")}}(tp[i]));
期望能有谁教我下更好的方法,但是有点失望了,好像没什么人回帖的。
<head>
<meta http-equiv = "content-type" content = "text/html" charset = "gb2312" >
<title>楼盘投票系统_Page1</title> </head>
<body>
<table>
<tr>
<td class="td2input">11111111111</td>
</tr>
<tr>
<td class="td2input">11111111111</td>
</tr>
<tr>
<td class="td2input">11111111111</td>
</tr>
<tr>
<td class="td2input">11111111111</td>
</tr>
</table>
<script>
function addEvent(obj, type, fn){
if (obj) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
var funIe = function(){
fn.call(window.event.srcElement);
};
obj.attachEvent("on" + type, funIe);
} else{
obj["on" + type] = fn;
}
};
} var removeEvent = function(obj, type, fn){
if(obj){
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + type, fn);
} else {
obj["on" + type] = null;
}
}
};
var ip = document.createElement("input"),currentCell;
var td2input = function(event) {
event == null ? currentCell=window.event.srcElement : currentCell=event.target;
ip.value=currentCell.innerHTML;
currentCell.innerHTML = "";
ip.onblur = blurDo;
currentCell.appendChild(ip);
ip.focus();
removeEvent(currentCell, "dblclick", td2input);
}; function blurDo(event) {
currentCell.innerHTML = ip.value;
if(event != null)
addEvent(currentCell, "dblclick", td2input);
} var i,j,tp = new Array();
var tdInput = document.getElementsByTagName("td");
for(i = 0, j = 0; i < tdInput.length; i++)
if(tdInput[i].className == "td2input")
tp[j++] = tdInput[i];
for(i = 0; i < tp.length; i ++)
addEvent(tp[i], "dblclick", td2input);
</script>
</body>
</html>
我这ie正常啊~·没看到js报错
确实不报错,但是你要是双击了之后,再次双击,就是在那个input里面再双击就会出问题了。
还有我在3楼写的代码也解决了,这个问题,但是我并没有移除侦听事件,而是判断是否已经添加了input来实现的,我现在就是想知道怎么移除所有的侦听事件。