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/

相关文章:

php - 使用 PHP 将 MySQL 数据导出到 xml - HTML 编码和一般错误 BRAIN MELT

javascript - jQuery:在所有计算和 DOM 更新完成后运行动画

html - 链接不能完全点击

css - 特殊页面背景 - 静态、平铺

html - 如何修复页脚?

css - 100% 高度内容 <div> 带有粘性页脚

javascript - IE上结合的Active状态伪类和相邻元素选择器

javascript - 目标 ="_blank"与 window.open

html - 如何将 CSS 文件连接到 HTML 文件?

css - 滚动内容溢出的 flexbox