对于一个input框,比如他已经绑定了一些onclick,onkeydown,onblur等事件,有什么好办法,能把这些已绑定的事件清除么?我用过input[i].onkeyup = null;input[i].detachEvent("onkeydown",XXX)但是感觉清楚不干净,谢谢了!

解决方案 »

  1.   

    给他一个disabled=true属性试一试
      

  2.   

    感谢LS我的本意是清楚他原来的一些绑定的事件,然后自己再绑定新的,disabled好像不行。。
      

  3.   

    你这些事件是怎么绑定的?直接赋值还是用attachEvent?
      

  4.   

    我是想清除任意的绑定事件无论他是直接写的onclick或者用attachEvent绑定的再或者什么有啥办法能一并封杀么
      

  5.   

    如果INPUT不多的话,卑鄙的做法是重写那些INPUT的HTML最彻底:
      

  6.   

    <input type="text" onclick="set()" onmouseover="set()" />
    <input type="text" onclick="set()" onmouseover="set()" />
    <input type="text" onclick="set()" onmouseover="set()" />
    <input type="text" onclick="set()" onmouseover="set()" />
    <input type="text" onclick="set()" onmouseover="set()" />
    <script>
    function set(){
    alert("fffff")
    }
    var obj=document.getElementsByTagName("input");
    for(i=0;i<obj.length;i++){
      obj[i].onclick=Function("return false")
      obj[i].onmouseover=Function("return false")
    }
    </script>
    是这样子的么
      

  7.   

    XXX.onclick="";就可以了吧,,,
      

  8.   

    感谢ls各位我现在是           input[i].onkeyup = null;
                    input[i].onkeydown = null;
                    input[i].onkeypress = null;
                    input[i].onfocus = null;
                    input[i].onblur = null;                    input[i].detachEvent("onkeydown",input[i].onkeydown);
                        input[i].detachEvent("onkeyup",input[i].onkeyup);
                        input[i].detachEvent("onkeypress",input[i].onkeypress);
                        input[i].detachEvent("onfocus",input[i].onfocus);
                        input[i].detachEvent("onblur",input[i].onblur);这样清除的感觉根本清除不干净
      

  9.   

    比如就说校内网把,他那里上面有个个人状态的记录框,无论我怎么清除绑定(textarea)鼠标点击此处会弹出个下拉框的js也清除不掉,(FF/IE都是如此)www.google.cn,用我说的那种方法,FF中google的下拉提示框是能清除的,但IE不行。。所以很混乱头疼啊。。求助。。
      

  10.   

    直接替换掉原来的东东,简单直接
    <!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>
        <title></title>
    </head>
    <body>
        <input id="tbTest" type="text" onclick="alert('click');" onfocus="alert('focus')"/>
        <input type="button" value="清空事件" onclick="Clear()" />
        <script language="javascript" type="text/javascript">
            var textBox = document.getElementById("tbTest");
            function Clear() {
                var newTextBox = document.createElement("input");
                newTextBox.type = "text";
                var parent = textBox.parentNode;
                parent.replaceChild(newTextBox, textBox);
                textBox = newTextBox;
            }
        </script>
    </body>
    </html>
      

  11.   

    要清除事件,你得找绑定函数的入口才行,如果人家绑定的是匿名函数,那根本清除不了的,能做的只能是,重新再新增一个事件,以前我测试过,越是后边绑定事件的函数会越先执行,因此你再新增一个事件,然后在ie中返回false ,就会停止事件监听了,在FF之类的调用事件的stopPropagation
      

  12.   

    不好意思,我修正一下自己错误,js中没有stopimmediatepropagation 的东西,因此有些绑定无法清除
      

  13.   

    jq1.3把所有段落的所有事件取消绑定jQuery 代码:
    $("p").unbind()
      

  14.   

    var input=document.getElementById('x');
    for (var v in input)
      if (typeof input[v]=='function')
        input[v]=null;这样可以么?
      

  15.   

    js也有垃圾收集机制的。<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
        <script type="text/javascript">        function body_click()
            {
                window.alert("body click");
                document.body.onclick = function() { };
            }
            
        </script>
    </head>
    <body onclick="body_click()">
    helloworld
    </body>
    </html>