<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
var foo = function(){alert('a')};
$(document).ready(function(){
$("#vv").bind("click",foo);
$("#aa").bind("click",function(){
     //如果有了事件就删除  没有事件就添加  该怎么写了
     //$("#vv").unbind("click",foo)
});
}
);
</script>
</head>
<body>
<div id="vv" style=" height:30px; background-color:#993366">asdfasdf</div>
<input id="aa" value="sadf" type="button" />
</body>

解决方案 »

  1.   

    不知道如何判断
    但是完成这中效果还是可以的
    <script type="text/javascript" src="jquery-1.3.2.js"> </script> 
    <script type="text/javascript"> 
    var foo = function(){alert('a')}; 
    $(document).ready(function(){ 
    $("#vv").bind("click",foo); 
    $("#aa").bind("click",(function(){
    var exit = 1;
    return function(){
    exit?$("#vv").unbind("click",foo):$("#vv").bind("click",foo)
    exit = exit?0:1;
    }
    })()); 

    ); 
    </script> 
    </head> 
    <body> 
    <div id="vv" style=" height:30px; background-color:#993366">asdfasdf </div> 
    <input id="aa" value="sadf" type="button" /> 
    </body>
      

  2.   

    if(document.getElementById("vv").onclick){  document.getElementById("vv").onclick=null;
       }  else {       document.getElementById("vv").onclick=foo;
       }
      

  3.   


    <script src="http://ajax.Microsoft.com/ajax/jQuery/jquery-1.3.2.min.js" type="text/javascript"> </script> 
    <script type="text/javascript"> 
    var foo = function(){alert('a')}; 
    $(document).ready(function(){ $("#vv").bind("click",foo);  $("#aa").bind("click",function(){
    var ev = $.data(document.getElementById("vv"), "events")["click"];
    if (ev[3]==foo) $("#vv").unbind("click",foo) 
    }); 
    }); 
    </script> 
    </head> 
    <body> 
    <div id="vv" style=" height:30px; background-color:#993366">asdfasdf </div> 
    <input id="aa" value="sadf" type="button" /> 
    </body> 
      

  4.   


        $("#aa").bind("click",function(){
            var ev = $('#vv').data('events')['click'], exists = false; 
            for(var i in ev) {
                if(ev[i] == foo) {
                    $('#vv').unbind('click', foo);
                    exists = true;
                    break;
                }
            }
            if(!exists) $('#vv').bind('click', foo);
        });