Bind a click to a dynamic button with jQuery?

后端 未结 3 2087
旧时难觅i
旧时难觅i 2021-01-24 17:32

I want to create a button that has a jQuery click on the fly. After the user is done and hit the button I want to destroy the button and the jQuery click until a time I need it

3条回答
  •  猫巷女王i
    2021-01-24 18:09

    Live would work just fine. If you'd like to avoid using live, you can wire up the the new button as you add it to the DOM.

    function addNewButton() {
      $("sweet_selector_here").append("");
      $("#sweetness").click(function() {
        $(this).remove();
      });
    }
    

    With live it becomes this:

    function addNewButton() {
      $("sweet_selector_here").append("");
    }
    
    $("#sweetness").live("click", function() {
      $(this).remove();
    });
    

提交回复
热议问题