如何向在 jquery 中动态创建的元素添加操作?

发布于 2024-11-30 08:21:46 字数 112 浏览 3 评论 0原文

我有一个按钮,当您单击此按钮时,它会动态创建第二个按钮。我需要在动态创建的按钮(第二个按钮)上放置一个单击事件,我这样做了,但我的单击功能不起作用。

如何让我的点击函数与动态创建的元素一起使用?

I have a button, when you click this button it dynamically creates a second button. I need to place a click event on to the dynamically created button (second button), which I do, but my click function isn't working.

How can I get my click function to work with dynamically created elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

妄断弥空 2024-12-07 08:21:46

您需要在创建第二个按钮后将单击事件绑定到它。或者使用 jQuery Live 或 Delegate 函数。

http://api.jquery.com/delegate/

将处理程序附加到与匹配的所有元素的一个或多个事件
选择器,现在或将来,基于一组特定的根
元素。

如果您需要更多帮助,请发布一些代码。

You either need to bind the click event to the second button after it's created. Or use the jQuery Live or Delegate functions.

http://api.jquery.com/delegate/

Attach a handler to one or more events for all elements that match the
selector, now or in the future, based on a specific set of root
elements.

Post some code if you need any more help.

枕花眠 2024-12-07 08:21:46

我知道这个问题很久以前就被问过......
从 jQuery v1.7 开始,正确的方法是:

$(document).on( "click",'.myNewlyCreatedElement', function () {
    // Your action
});

参考:http://api.jquery.com/on /

I know this question was asked a long while ago...
Since jQuery v1.7 the correct way to do that is :

$(document).on( "click",'.myNewlyCreatedElement', function () {
    // Your action
});

Ref : http://api.jquery.com/on/

风轻花落早 2024-12-07 08:21:46

您必须使用 jQuery .live() 函数,可以在此处

例子:

$('button').live('click', function(){
    ...
});

You have to use the jQuery .live() function which can be found here.

Example:

$('button').live('click', function(){
    ...
});
雅心素梦 2024-12-07 08:21:46

这是一个示例

标记:

<button id="create">Create</button>

<div id="container">
</div>

脚本:

var count = 0;
$("#create").click(function(e) {
    var num = count += 1;
    var newButton = $("<button>New " + num + "</button>").click(function() {
        alert("Button " + num + " was clicked.");
    });
    $("#container").append(newButton);
});

Here is an example.

Markup:

<button id="create">Create</button>

<div id="container">
</div>

Script:

var count = 0;
$("#create").click(function(e) {
    var num = count += 1;
    var newButton = $("<button>New " + num + "</button>").click(function() {
        alert("Button " + num + " was clicked.");
    });
    $("#container").append(newButton);
});
━╋う一瞬間旳綻放 2024-12-07 08:21:46

查看 jQuery .live()

jQeru .live()

check out jQuery .live()

jQeru .live()

温柔嚣张 2024-12-07 08:21:46

您需要使用 jqueries live 方法附加一个事件。它将事件绑定到当前和未来元素的所有实例。

$('.classname').live('click', function() {
  alert("triggered");
});

这样,单击事件就会添加到类名为 classname 的所有元素中 - 即使您插入更多具有相同类的元素。

you need to attach an event using jqueries live method. It will bind the event to all instances of current and future elements.

$('.classname').live('click', function() {
  alert("triggered");
});

this way the click event is added to all elements with the classname of classname - even if you insert more elements with the same class.

甜柠檬 2024-12-07 08:21:46
<input id="but1" type="button" value="click"></input>
<script>
$(document).ready(function(){
    $('#but1').click(function(){
    var button = $(document.createElement('input'));
        button.attr('id', 'but2');
        button.val('but2');
        button.attr('type', 'button');
        button.click(function(){alert('hello');});

        $(document.body).append(button);

    });

});
</script>
<input id="but1" type="button" value="click"></input>
<script>
$(document).ready(function(){
    $('#but1').click(function(){
    var button = $(document.createElement('input'));
        button.attr('id', 'but2');
        button.val('but2');
        button.attr('type', 'button');
        button.click(function(){alert('hello');});

        $(document.body).append(button);

    });

});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文