この関数を使用してボタンでdivを動的に作成しています
var counter = 1;
$("#button1").click(function(){
$("<div/>", {
"class": "test" + (counter++),
text: "",
}).resizable().draggable()
.appendTo("body");
});
これらの動的に作成された div を削除するには、別のボタンを追加するにはどうすればよいですか?
この関数を使用してボタンでdivを動的に作成しています
var counter = 1;
$("#button1").click(function(){
$("<div/>", {
"class": "test" + (counter++),
text: "",
}).resizable().draggable()
.appendTo("body");
});
これらの動的に作成された div を削除するには、別のボタンを追加するにはどうすればよいですか?
もちろん、動的に追加された各要素 (この場合は.dynamic
. 次に、別のボタンが押されると、そのクラスのインスタンスが削除されます。
var counter = 1;
$("#button1").click(function(){
$("<div/>", {
"class": "dynamic test" + (counter++), // note we're adding a new generic class
text: "",
}).resizable().draggable()
.appendTo("body");
});
$("#button2").click(function(){
$(".dynamic").remove();
});
$("#button1").click(function(){
$("<div/>", {
"class": "dynamic test" + (counter++),
text: "",
}).append('<div id="button"' + (counter -1) + '">Close</div>').resizable().draggable()
.appendTo("body");
$("#button" + (counter -1)).click(function(){
$(".test" + (counter-1)).remove();
});
});
そのdivを閉じるために、作成された各divに閉じるボタンを追加することもできます