Javascript Jquery:仅在“单击时”追加一次 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34476696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:23:40  来源:igfitidea点击:

Jquery: Append div only once 'on click'

javascriptjqueryhtml

提问by Tauseef Hussain

I am trying to append a sectionto a divon the click of a button.

我试图通过单击按钮将 a 附加section到 a div

I want the sectionto be appended only on the first click, the code that I use below appends a sectionevery time I click the div.

我希望section仅在第一次单击时附加,我在下面使用的代码section每次单击div.

How should I go about this?

我应该怎么做?

$("#nextractorapply").click(function () {
    $("#main").append('<section id="nextractor" class="five"> </section>');  
});

回答by adeneo

You could use one(), which fires only once

你可以使用one(),它只触发一次

$("#nextractorapply").one('click', function () { 
    // executes only once

     $("#main").append('<section id="nextractor" class="five"> </section>');  
});

$("#nextractorapply").on('click', function () { 
    // executes every time

    // do other stuff
});

回答by Donnie D'Amato

Use a conditional to determine if it's there.

使用条件来确定它是否存在。

$("#nextractorapply").click(function () {
    if($('#nextractor').length < 0){
        $("#main").append('<section id="nextractor" class="five"> </section>');
    }  
});

回答by Hemal

You can use some kind of a condition that prevents its appending multiple times.

您可以使用某种条件来防止其多次附加。

var counter=0;
$("#nextractorapply").click(function () {
    if(counter<=0){
        $("#main").append('<section id="nextractor" class="five"> </section>');  
        counter++;
    }

    //YOUR OTHER STUFF THAT YOU NEED TO EXECUTE ON EVERY CLICK
});

回答by Amar Singh

You could use .unbind()

你可以用 .unbind()

$("#nextractorapply").unbind().click(function () {
    $("#main").append('<section id="nextractor" class="five"> </section>');  
});

回答by Amirhossein Tarmast

You could check if the element has a default class, append html. For example you could add a class to html code like : class="first", and after first click remove that class.

您可以检查元素是否具有默认类,附加 html。例如,您可以向 html 代码添加一个类,例如:class="first",然后在第一次单击后删除该类。

A code something like this :

像这样的代码:

var element = $("#nextractorapply");
    if(element.hasClass("first")){
          $("#main").append('<section id="nextractor" class="five"> </section>');  
          element.removeClass("first");
    }

And then after first click, the "first" class will be removed from your html and Jquery will not append any further html.

然后在第一次单击后,“第一个”类将从您的 html 中删除,并且 Jquery 不会附加任何进一步的 html。

回答by ronangr1

You could set a variable and fire event if variable is set as true or false.

如果变量设置为 true 或 false,您可以设置变量并触发事件。

var _clicked = false;

$('.element').click(function(){
  if(!_clicked){
   _clicked = true;
   console.log('event fired');
  }
})