简体   繁体   English

简单但奇怪的HTML,jQuery问题。 功能循环无故

[英]Simple but strange HTML, jQuery issue. Function loops with no reason

I wrote the code in javascript (jQuery), that allows a user with every click of a button to create a "box" on the web site and to get an alert message after this box was clicked. 我用javascript(jQuery)编写了代码,该代码使用户每次单击按钮都可以在网站上创建一个“框”,并在单击该框后获得警报消息。

It works like this: 1) When the user presses the button "Add (#addBox)" - jQuery appends a new line to the HTML file, that creates the "box" (it looks like a box because of the CSS code). 它的工作方式如下:1)当用户按下“添加(#addBox)”按钮时-jQuery将一个新行添加到HTML文件中,从而创建“框”(由于CSS代码,它看起来像一个框)。 2) If the user presses the box, it sends out the alert message "Hello". 2)如果用户按下该框,它将发出警报消息“ Hello”。

But if I add multiple boxes and then click on the first one, instead of sending out one alert message, it sends it out as many time as the number of boxes been created. 但是,如果我添加多个框,然后单击第一个框,而不是发送一条警告消息,它会发送与创建框数量一样多的时间。

Example: 1) I have 1 box. 示例:1)我有1个盒子。 By pressing on it, I receive 1 alert message. 通过按下它,我收到1条警告消息。 2) I have 2 boxes. 2)我有2个盒子。 By pressing on the top one, I receive 2 alert messages. 通过按顶部的一个,我收到2条警报消息。 By pressing on the second one, I receive 1 message. 通过按第二个,我收到1条消息。 3) I have 3 boxes. 3)我有3个盒子。 By pressing on the top one, I receive 3 alert messages. 通过按顶部的一个,我收到3条警报消息。 By pressing on the second one, I receive 2 messages. 通过按第二个,我收到2条消息。 By pressing on the third one, I receive 1 message. 通过按第三个,我收到1条消息。

The function of sending an alert message is looping for some reason. 由于某些原因,发送警报消息的功能正在循环。 And so here is the code: 所以这是代码:

function addBox()
{
    $("#addBox").on("click", function () {
        $("#addBox").append('<div class="desiredBox">Say Hello</div>');
        }

        boxCount();
    });
}

function boxCount()
{
    $(".desiredBox").on("click", function () {
        alert("Hello");
    });
}

Any ideas, how to make them send only one message each, preventing the function "boxCount()" from looping? 有什么想法,如何使它们每个仅发送一条消息,从而防止函数“ boxCount()”循环?

Every time the function boxCount is invoked an event handler is added to existing elements ie ".desiredBox" . 每次调用boxCount函数时, boxCount将事件处理程序添加到现有元素(即".desiredBox" thus you are getting multiple alerts. 因此,您将收到多个警报。

As you are creating elements dynamically. 在动态创建元素时。

You need to use Event Delegation . 您需要使用事件委托 You have to use .on() using delegated-events approach. 您必须使用.on()使用委托事件方法。

General Syntax 一般语法

$(document).on(event, selector, eventHandler);

Ideally you should replace document with closest static container. 理想情况下,应使用最近的静态容器替换document

Complete code 完整的代码

$("#addBox").on("click", function () {
    $("#addBox").append('<div class="desiredBox">Say Hello</div>');        
});
$(document).on('click', '.desiredBox', function(){
  //Your code
});

Use event delegation so you don't keep adding the click event to .desiredBox over and over again: 使用事件委派,这样就不必一遍又一遍地将click事件添加到.desiredBox

$(document).on("click", "#addBox", function () {
    $("#addBox").append('<div class="desiredBox">Say Hello</div>');
    $(".desiredBox").trigger("click");
});


$(document).on("click", ".desiredBox", function () {
    alert("Hello");
});

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

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