简体   繁体   English

附加 <div> 单击并单击删除

[英]Append <div> with click and remove with click

I have the following problem: 我有以下问题:

I append the div: 我追加了div:

$(".class").click(function() {   
    $(this).append("<div class='click'></div>");
    $("div.click").show();
});

Then i remove it with a click on another button but the div is still there. 然后我单击另一个按钮将其删除,但div仍然存在。

$(".button").on("click", function(e){
    e.preventDefault();
    ...
    $("div.click").hide();
});

Try keeping a pointer to the div the following should work. 尝试保持指向div的指针,以下方法应该起作用。

var tempDiv;
$(".class").click(function() {   
  tempDiv = $("<div class='click'></div>").appendTo($(this)).show();
});

$(".button").on("click", function(e){
  e.preventDefault();
  tempDiv.remove();
});

Otherwise you can use this way 否则你可以用这种方式

$(".class").click(function() {   
   $("<div class='click'></div>").appendTo($(this)).show();
});

$(".button").on("click", function(e){
  e.preventDefault();
  $('.click').remove();
});

PS: You may also remove the .show() if the .click class is not hidden by default PS:您也可以删除.show()如果.click类没有默认隐藏

Try this

You have two buttons.

Say:

<div class="Main">
    <div>Div0</div>
</div>

<button class="button1">Click to add</button>
<button class="button2">Click to remove</button>


and JS Code is :

var counter=1;

$(".button1").click(function() {  

$('.Main').append("<div class='click'> newly added Div "+counter+"</div>");
counter++;
$("div .click").show();
});

$(".button2").click(function() {

$('.Main div').remove(':last-child');
});

Here is an example based on your work : http://jsfiddle.net/UQTY2/128/ 这是一个基于您的工作的示例: http : //jsfiddle.net/UQTY2/128/

<div class="class">Click to add a green box</div>
<button class="button">Click to remove all green boxes</button>

$(".class").click(function() {
    $(this).append("<div class='click'></div>");
});

$(".button").click(function(e) {
    e.preventDefault();
    $("div.click").remove();
});

this will remove 这将删除

$(".button").on("click", function (e) {

    e.preventDefault();
    $("div.click").remove();
});

check my fiddle 检查我的小提琴

http://jsfiddle.net/suhailvs/4VmYP/2/ http://jsfiddle.net/suhailvs/4VmYP/2/

When you dynamicly create element, you need delegated-event: .on( event, selector, handler(eventObject) ) . 动态创建元素时,需要委托事件: .on( event, selector, handler(eventObject) )

$(document).on("click", ".button", function(e){
    e.preventDefault();
    ...
    $("div.click").hide();
});

If you want remove element, you shoud use .remove() method instead of .hide() . 如果要删除元素,则应使用.remove()方法而不是.hide()

you can dynamically add and remove div with javaScript like this 您可以像这样使用javaScript动态添加和删除div

Check this example 检查这个例子

Add and Remove Div dynamically 动态添加和删除Div

in this example the default remove button remove the most recent added div or you can say the last div in the container But if you want to remove particular div with div place number you can enter the div number . 在此示例中,默认的删除按钮将删除最近添加的div,或者可以说出容器中的最后一个div。但是,如果要删除带有div地名的特定div,则可以输入div编号。

Code example 代码示例

HTML 的HTML

<div class="Main">
    <div>div1</div>
</div>
<button id="ok">add</button>
<button id="del">remove</button>
<label>Enter div number to remove</label>
<input id="V"/>
<button id="Vok">ok</button>

JS JS

var counter=0;
$("#ok").click(function(){
    $('.Main').append('<div> new div'+counter+'</div>');
    counter++;
})
$("#del").click(function(){
    $('.Main div').remove(':last-child');
})
$("#Vok").click(function(){
    var Val=$('#V').val();
    $('.Main div:nth-child('+Val+')').remove();
})

remove "on" from 从中删除“ on”

$(".button").on("click", function(e){
    e.preventDefault();
    ...
    $("div.click").hide();
});

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

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