html - 我怎样才能做一个删除按钮?

标签 html css

所以我制作了一个按钮,当我点击它时会生成随机卡片。当我点击卡片时,它会显示两个文本字段和两个按钮。

现在我想在随机卡上制作一个按钮(在我的代码中按钮应该是“x”)以删除随机卡。遗憾的是,这不起作用。你们能帮帮我吗?

这是我的代码:

$(document).ready(function() {
  $("button.plus").on("click", function() {
    var newCard = $('#cardPrototype').clone(true);
    $(newCard).css('display', 'block').removeAttr('id');
    $('#newCardHolder').append(newCard);
  });

  $('body').on('click', '.card', function() {
    $(this).find('form').show();
    $(this).find('span').remove();

  });
});

/*delete button*/
$('body').on('click', '.card , ".delete', function() {
  $(this).closest('.card').remove();
});
.input-feld {
  font-family: TheSans Swisscom;
  margin: 3px;
  width: 260px;
}
.card {
  width: 300px;
  margin-right: 5px;
  margin-left: 5px;
  margin-bottom: 10px;
  float: left;
  padding: 10px;
  background-color: #BBBBBB;
  border: 1px solid #ccc;
  border-radius: 10px;
  position: relative;
}
.delete {
  font-family: 'TheSans Swisscom';
  right: 12px;
  top: 0;
  position: absolute;
}
.speichern {
  font-family: 'TheSans Swisscom';
  background-color: greenyellow;
}
.abbrechen {
  font-family: "TheSans Swisscom";
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="plus">
  <div class="item">
    <p>+</p>

  </div>
</button>

<div id="newCardHolder">

</div>

<div id="cardPrototype" class="card" style="display:none;">
  <p class="delete">x</p>
  <span>Title</span>
  <form name="theform" style="display:none;">
    <input class="input-feld" type="text">
    <br>
    <input class="input-feld " type="text">
    <br>
    <input class="speichern" type="button" onClick="new Person()" value="Speichern">
    <input class="abbrechen" type="button" onClick="new Person()" value="Abbrechen">
  </form>
</div>

最佳答案

你的删除选择器是错误的,尝试.card .delete:

$(document).ready(function() {
  $("button.plus").on("click", function() {
    var newCard = $('#cardPrototype').clone(true);
    $(newCard).css('display', 'block').removeAttr('id');
    $('#newCardHolder').append(newCard);
  });

  $('body').on('click', '.card', function() {
    $(this).find('form').show();
    $(this).find('span').remove();

  });

  /*delete button*/
  $('body').on('click', '.card .delete', function() {
    $(this).closest('.card').remove();
  });
});
.input-feld {
  font-family: TheSans Swisscom;
  margin: 3px;
  width: 260px;
}
.card {
  width: 300px;
  margin-right: 5px;
  margin-left: 5px;
  margin-bottom: 10px;
  float: left;
  padding: 10px;
  background-color: #BBBBBB;
  border: 1px solid #ccc;
  border-radius: 10px;
  position: relative;
}
.delete {
  font-family: 'TheSans Swisscom';
  right: 12px;
  top: 0;
  position: absolute;
}
.speichern {
  font-family: 'TheSans Swisscom';
  background-color: greenyellow;
}
.abbrechen {
  font-family: "TheSans Swisscom";
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="plus">
  <div class="item">
    <p>+</p>

  </div>
</button>

<div id="newCardHolder">

</div>

<div id="cardPrototype" class="card" style="display:none;">
  <p class="delete">x</p>
  <span>Title</span>
  <form name="theform" style="display:none;">
    <input class="input-feld" type="text">
    <br>
    <input class="input-feld " type="text">
    <br>
    <input class="speichern" type="button" onClick="new Person()" value="Speichern">
    <input class="abbrechen" type="button" onClick="new Person()" value="Abbrechen">
  </form>
</div>

关于html - 我怎样才能做一个删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39225610/

相关文章:

javascript - 为参数的条件编写 If 语句

html - 在 bootstrap 3.3.5 中更改导航宽度(从 1170px 到 940px)

javascript - 如何手动更改 CDEditor 的边框和背景样式(颜色、 View ...)?

jquery - 右浮动的图片在调整容器大小时跳动

javascript - 使用变量 + 按钮更改图像位置

css - 无法在 HTML 中创建下拉列表

jquery - 一篇文章中有多个图像,但全部在一个容器中

html - 如何添加覆盖主导航标题的粘性标题?

javascript - 如何使用 HtmlService 运行服务器端功能

html - 当最后一个单词后跟一个连字符时,对齐单行会在 IE 中的最后一个单词之前换行