我有添加/删除按钮的输入部分,添加额外的输入字段.我正在try 让输入区域出现一个过渡效果,而不是立即弹出.

我试着用transition,但没用.是否有可能通过纯css来实现这一点?

代码

var i = 1;

$('#add_patient').click(function() {
  i++;
  $('#tbl_patient').append('<tr id="row' + i + '" class="dynamic-added">\
  <td>\
     <input type="text" id="patient' + i + '" name="patient" placeholder="Patient Name" required>\
  </td>\
  <td>\
     <button class="btn_remove" type="button" name="remove" id="' + i + '">--</button>\
  </td>\
</tr>');
});

$(document).on('click', '.btn_remove', function() {
  var button_id = $(this).attr("id");
  $('#row' + button_id + '').remove();
});
td {
  transition: 0.5s ease 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl_patient">
  <thead>
    <tr>
      <th colspan="2" class="uk-text-center">Patient</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" id="patient" name="patient" placeholder="Patient Name">
      </td>
      <td>
        <button type="button" name="add_patient" id="add_patient">+</button>
      </td>
    </tr>
  </tbody>
</table>

推荐答案

我没有使用jQuery,所以不得不使用一些普通的Java脚本,但本质上是将过渡应用于表行,并使用从0到1的opacity来创建淡入效果.

var i = 1;

$('#add_patient').click(function() {
  i++;
  $('#tbl_patient').append('<tr id="row' + i + '" class="dynamic-added hidden">\
  <td>\
     <input type="text" id="patient' + i + '" name="patient" placeholder="Patient Name" required>\
  </td>\
  <td>\
     <button class="btn_remove" type="button" name="remove" id="' + i + '">--</button>\
  </td>\
</tr>');

setTimeout(()=>{
  document.querySelector('tr.hidden:last-of-type').classList.remove('hidden');
},100);
});

$(document).on('click', '.btn_remove', function() {
  var button_id = $(this).attr("id");
    $('#row' + button_id).remove();
});
tr{
  transition:0.5s ease-in-out all;
  opacity:1;
}
.hidden{
  opacity:0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tbl_patient">
  <thead>
    <tr>
      <th colspan="2" class="uk-text-center">Patient</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" id="patient" name="patient" placeholder="Patient Name">
      </td>
      <td>
        <button type="button" name="add_patient" id="add_patient">+</button>
      </td>
    </tr>
  </tbody>
</table>

Javascript相关问答推荐

确定MutationRecord中removedNodes的索引

如何在mongoose中链接两个模型?

在Vite React库中添加子模块路径

切换时排序对象数组,切换不起作用

从Node JS将对象数组中的数据插入Postgres表

构造HTML表单以使用表单数据创建对象数组

在执行异步导入之前判断模块是否已导入()

使用原型判断对象是否为类的实例

在我的index.html页面上找不到我的Java脚本条形图

如何将数组用作复合函数参数?

在数组中查找重叠对象并仅返回那些重叠对象

未加载css colored颜色 ,无法将div设置为可见和不可见

根据一个条件,如何从处理过的数组中移除一项并将其移动到另一个数组?

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

构建器模式与参数对象输入

我怎样才能得到一个数组的名字在另一个数组?

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

在没有任何悬停或其他触发的情况下连续交换图像

在点击链接后重定向至url之前暂停

此上下文在JavaScript中