简体   繁体   English

添加一个可用的按钮到.AppendTo - Jquery

[英]Adding a usable button to .AppendTo - Jquery

Basically I've created a overlay and load this into appendTo as a 'loading screen'. 基本上我已经创建了一个叠加层并将其加载到appendTo作为“加载屏幕”。

Recently, I needed to add a Stop button to the overlay which isn't a problem, however the jquery .click function isn't picking up the button click, I've tried keeping the existing button and therefore it would maybe register, but sadly hasn't. 最近,我需要在叠加层上添加一个停止按钮,这不是问题,但是jquery .click函数没有拿起按钮点击,我已经尝试保留现有按钮,因此它可能会注册,但是可悲的是没有。

(I've also tried using an Id for the button too. (我也尝试过使用Id作为按钮。

Below is some test code to demonstrate the problem. 下面是一些演示问题的测试代码。

$(document).ready(function(){
    $("#addButton").click( function () {
        $overlay = $("<div id='overlay'><div id='contain'><h3 style='color:#FFF'>Processing, just a moment</h3> <div id='stop'><button type='button' class='btn btn-primary' id='stop1' >Stop</button></div><div class='bar' style='width: 80%; padding-top:50%'></div></div></div");
        $overlay.appendTo(document.body);
        $("#stop").click( function() {
            alert("working");
        });
    });

Edit: 编辑:

Just to clarify, this is what I originally wanted, however due to 'over changes' the above example will work in my scenario, this is the original problem just for clarity. 只是为了澄清,这是我最初想要的,但是由于“过度改变”,上面的例子将在我的场景中起作用,这只是为了清晰起见的原始问题。

    $(document).ready(function(){
        $("#addButton").click( function () {
            $overlay = $("<div id='overlay'><div id='contain'><h3 style='color:#FFF'>Processing, just a moment</h3> <div id='stop'><button type='button' class='btn btn-primary' id='stop1' >Stop</button></div><div class='bar' style='width: 80%; padding-top:50%'></div></div></div");
            $overlay.appendTo(document.body);

        });

$("#stop").click( function() {
                alert("working");
            });

});

Yes, you can do that too. 是的,你也可以这样做。 See the docs for the on() method . 请参阅on()方法文档

$(document).ready(function(){
    $("#addButton").click( function () {
        $overlay = $("<div id='overlay'><div id='contain'><h3 style='color:#FFF'>Processing, just a moment</h3> <div id='stop'><button type='button' class='btn btn-primary' id='stop1' >Stop</button></div><div class='bar' style='width: 80%; padding-top:50%'></div></div></div");
        $overlay.appendTo(document.body);
    });

    $(document).on("click", "#stop", function() {
        alert("working");
    });
});

This adds only one click listener to the document, which fires every time a click event bubbles from a #stop element. 这只会向文档添加一个单击侦听器,每次单击事件从#stop元素冒泡时都会触发该单击侦听器。 However, this is usually only needed for many elements, so you should better use a class " stop " when there's more than one such button. 但是,这通常只需要很多元素,因此当有多个这样的按钮时,最好使用“ stop ”类。

Yet, I guess you will need the closure which you had in your first example to execute the right onclick-actions. 但是,我想你需要在第一个例子中使用闭包来执行正确的onclick-actions。

$(document).ready(function(){
    $("#addButton").on('click', function() {
        $overlay = $("<div id='overlay'><div id='contain'><h3 style='color:#FFF'>Processing, just a moment</h3> <div id='stop'><button type='button' class='btn btn-primary' id='stop1' >Stop</button></div><div class='bar' style='width: 80%; padding-top:50%'></div></div></div");
        $overlay.appendTo(document.body);
    });

    $(document).on('click', '#stop', function() {
        alert("working");
    });
});

Use .live() so that anything newly created will still get the click event. 使用.live()以便新创建的任何内容仍然会获得click事件。
http://jsfiddle.net/DerekL/2GCfD/ http://jsfiddle.net/DerekL/2GCfD/

在此输入图像描述

It is working now. 它现在正在运作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM