简体   繁体   English

jQuery click只工作一次

[英]jQuery click works only once

I want to create a circle everytime I click the button but the once I click it, it creates a circle but when i click it again nothing happen. 我想在每次单击按钮时创建一个圆圈,但是一旦我点击它,它就会创建一个圆圈,但是当我再次点击它时,没有任何事情发生。

 $(document).ready(function() { var circle = $("<div class='circleClass'></div>"); $(".t-testbody").on("click", "#clickMe", function() { $(".t-testbody").append(circle); }); }); 
 .t-testbody { margin-top: 100px; margin-left: 50px; } .circleClass { width: 50px; height: 50px; border-radius: 100%; background-color: blue; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="t-testbody"> <div class="circleClass"></div> <button id="clickMe">Button</button> </div> 

Currently you have created the element and appended it to the div. 目前,您已创建元素并将其附加到div。 so second append statement has not effect as the element already exist in the div. 所以第二个附加语句没有效果,因为div中已存在该元素。

Instead of element use HTML string 而不是元素使用HTML字符串

var circle = "<div class='circleClass'></div>";
$(".t-testbody").on("click", "#clickMe", function () {
    $(".t-testbody").append(circle);
});

DEMO DEMO


You can use .clone() 你可以使用.clone()

var circle = $("<div class='circleClass'></div>");
$(".t-testbody").on("click", "#clickMe", function () {
    $(".t-testbody").append(circle.clone());
});

DEMO DEMO

You are defining your HTML element only once, so instead of this 您只定义了一次HTML元素,因此不是这样

$(document).ready(function() {
  var circle = $("<div class='circleClass'></div>"); // Move this into event handler callback
  $(".t-testbody").on("click", "#clickMe", function() {
    $(".t-testbody").append(circle);
  });
});

Do this: 做这个:

$(document).ready(function() {
  $(".t-testbody").on("click", "#clickMe", function() {
    var circle = $("<div class='circleClass'></div>"); // Move this here
    $(".t-testbody").append(circle);
  });
});

What's happening is that jQuery creates the HTML element, then on click it moves that element to the div. 发生了什么是jQuery创建HTML元素,然后点击它将该元素移动到div。 When you click it again, it moves that same element into where it just was, giving the illusion that it did nothing, but it just moved it into the position it already was. 当你再次点击它时,它会将相同的元素移动到它刚才的位置,给人一种错觉,它什么也没做,但它只是将它移动到它已经存在的位置。

When you move the variable declaration into the callback, it will generate a new html element every time you click that element, therefore jQuery will be appending a newly defined element to the div. 当您将变量声明移动到回调中时,每次单击该元素时它都会生成一个新的html元素,因此jQuery会将新定义的元素附加到div。

circle holds the reference of element being appended. circle保存附加元素的引用。 So it has no difference after first click. 所以首次点击后没有区别。

You can create circle inside the callback function like this : 你可以在回调函数中创建一个圆圈,如下所示:

 $(document).ready(function(){ $(".t-testbody").on("click","#clickMe",function(){ var circle = $("<div class='circleClass'></div>"); $(".t-testbody").append(circle); }); }); 
 .t-testbody { margin-top: 100px; margin-left: 50px; } .circleClass { width: 50px; height: 50px; border-radius: 100%; background-color: blue; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="t-testbody"> <div class="circleClass"></div> <button id="clickMe">Button</button> </div> 

Demo : http://jsfiddle.net/vikashvverma/ou52j2xn/ 演示: http//jsfiddle.net/vikashvverma/ou52j2xn/

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

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