+ -
当前位置:首页 → 问答吧 → js实现可编辑表格问题。

js实现可编辑表格问题。

时间:2011-09-07

来源:互联网

先发我写的代码
JScript code

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掉事件。

或者有没有什么方法,可以获取某个元素的所有侦听事件?

作者: a34020249   发布时间: 2011-09-07

每次双击都要检测innerHTML中是否存在着某个字符串(如:input)

作者: kyzy_yy_pm   发布时间: 2011-09-07

引用 1 楼 kyzy_yy_pm 的回复:

每次双击都要检测innerHTML中是否存在着某个字符串(如:input)

嗯,我也想过这样做,但是侦听事件是冒泡的,在chrome中你的双击就会变成在input中了。

IE好像不是,我想最好还是能够移除侦听事件。

作者: a34020249   发布时间: 2011-09-07

热门下载

更多