javascript - 添加和删​​除按钮

标签 javascript html

我目前正在为学校作业开发一个简单的网页,我想知道是否可以创建一个按钮来添加一个按钮,并在其旁边创建一个按钮来删除该按钮?

基本上是一个名为“添加按钮”的按钮,如果您单击它,则会出现一个具有相同大小的按钮(可以命名为任何适当的名称,不需要任何功能)。无论我点击多少次,都会添加更多按钮,例如按钮1、按钮2、按钮3等。此外,“添加按钮”按钮旁边是一个名为“删除按钮”的按钮,它可以按顺序删除按钮,例如从按钮3,按钮2,然后按钮1

最佳答案

这样的事情怎么样?稍微改变增量。

  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <style>
   button{
     height:100px;
     width: 80px;
     text-align: center;
     margin: 5px 4px;
   }
  #mainButtons button{
    display: inline-block;
    margin: auto;
    }
   </style>


<div id="mainButtons">
 <button onclick="addButton()">Make Button</button>
 <button onclick="removeButton()">Remove Button</button>
</div>
</br>
<div id="que"></div>

<script>
var increment = 0;
function addButton(){
 $("#que").append($("<button id='btn"+(increment++)+"'>This is Button"+ (increment)+"</button>"));
}

function removeButton(){
  $("#btn"+(increment-1)).remove();
  increment--;
}
</script>

关于javascript - 添加和删​​除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45763519/

相关文章:

javascript - 第一次在 javascript/jquery 中运行函数时没有做任何事情

html - :hover border-style:none not sticking to image within SSI

html - 将鼠标悬停在底部时更改 div 的高度

html - 水平滚动并排放置 li 元素

javascript - Angular 脚本中的传单更改图标

javascript - 在 angularjs 中加载 json

javascript - 将 `this` 传递给访问其属性的函数给出未定义?

javascript - react 美丽的 DND - 我得到 "Unable to find draggable with id: 1"

javascript - 数据表标签搜索

html - 菜单一直是 "clicked"。悬停效果出错