3

この関数を使用してボタンでdivを動的に作成しています

var counter = 1;
$("#button1").click(function(){
 $("<div/>", {
   "class": "test" + (counter++),
    text: "",
  }).resizable().draggable()
  .appendTo("body");
});

これらの動的に作成された div を削除するには、別のボタンを追加するにはどうすればよいですか?

4

2 に答える 2

3

もちろん、動的に追加された各要素 (この場合は.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();
});
于 2012-04-25T08:38:20.357 に答える
1
$("#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に閉じるボタンを追加することもできます

于 2012-04-25T08:42:56.407 に答える