使用jQuery将点击绑定到动态按钮吗? [英] Bind a click to a dynamic button with jQuery?

查看:62
本文介绍了使用jQuery将点击绑定到动态按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有jQuery单击的按钮.在用户完成操作并单击按钮后,我要销毁按钮,然后单击jQuery,直到需要重新创建它为止.

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 recreated.

我不确定如何在jQuery中执行此操作.我知道jQuery.live是一个选项,但是我不确定这会比我想要的方法好还是坏.

I am not sure how to do this in jQuery. I know jQuery.live is an option but I am not sure if this would be better or worse then the way I want to do it.

推荐答案

Live可以正常工作.如果您不想使用live,可以在将新按钮添加到DOM时将其连接起来.

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("<input type='button' id='sweetness' value='press me, i am awesome' />");
  $("#sweetness").click(function() {
    $(this).remove();
  });
}

通过直播变成这样:

function addNewButton() {
  $("sweet_selector_here").append("<input type='button' id='sweetness' value='press me, i am awesome' />");
}

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

这篇关于使用jQuery将点击绑定到动态按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆