search

Home  >  Q&A  >  body text

javascript - Recently, when processing business logic, I unbind the same element first and then bind again, and found a usage problem.

After unbinding first, bind sometimes fails. Some business requirements no longer need to be bound, and some need to be re-bound.
The following is a simple example

window.onload=function(){
   $("input").unbind();
}            
$("input").bind("click",function(){
   alert("1");
})

This cannot be rebinded

Written like this, you can bind

        $("input").unbind();
        $("input").bind("click",function(){
            alert("1");
        })

Ask the reason for this problem? Expert analysis

某草草某草草2778 days ago965

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-26 11:00:53

    The execution order is different. The unbind in onload is executed after the bind below, so you bind first, then unbind after onload

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-26 11:00:53

    You bound a click event to the input, but the click event was removed from your window.onload

    reply
    0
  • Cancelreply