我有很多列表,我需要在刪除一個(gè) li 后更新它們的總數(shù),代碼運(yùn)行良好但在刪除 li 后它沒有更新,那么如何在刪除一個(gè)后更新 li 的總數(shù)?我花了很長(zhǎng)時(shí)間找到解決方案,但我失敗了!var totalList = document.getElementById('total');var deleteBtn = document.getElementsByClassName('delete');var allList = document.querySelectorAll(".navbar-nav li");var removeNotify = function(e) { e.preventDefault(); var notifyItem = $(e.delegateTarget).parent(); var html = $('html'); html.css('overflowX', 'hidden'); notifyItem.addClass(notifyItem.data('animation-class')); notifyItem.bind('oanimationend animationend webkitAnimationEnd', function() { notifyItem.remove(); html.removeAttr('style'); }); };$(function() { $('.delete').on('click', removeNotify); console.log(allList.length - 1); });// total livar myList = document.getElementById('myList');$('.delete').on('click', 'li', function(ev){ totalList.innerHTML = allList.length - 1}); <div class="notifyNum align-center d-flex justify-content-center align-items-center mr-auto"> <span id="total" class="fontSize12"></span> </div><ul id="myList" class="navbar-nav w-100 align-left"> <li class="nav-item title"> <h1 class="p-0 m-0 weight700 fontSize20 gray6">Notifications</h1> </li> <li id="1" class="nav-item" data-animation-class="minimize"> <h2 class="notifyTitle weight600 fontSize16 gray5 mb-2">test-1 </h2> <span class="delete text-center">×</span> </li> <li id="2" class="nav-item" data-animation-class="minimize"> <h2 class="notifyTitle weight600 fontSize16 gray5 mb-2">test-2 </h2> <span class="delete text-center">×</span> </li> <li id="3" class="nav-item" data-animation-class="minimize"> <h2 class="notifyTitle weight600 fontSize16 gray5 mb-2">test-3 </h2> <span class="delete text-center">×</span> </li> </ul>
通過單擊按鈕刪除其中一個(gè)后更新 li 的數(shù)量
智慧大石
2023-03-24 16:05:40