1 回答
TA貢獻1811條經驗 獲得超6個贊
首先,你的 HTML 是無效的,因為你有開始label標簽而不是結束標簽。接下來,不要在 HTML 中進行內聯事件處理,而是將其分開并在 JavaScript 中進行。
對于您的問題,只需在刪除 div 后循環標簽,并使用標簽集合中的索引位置更新其文本:
// Set up event handlers on each of the child divs.
// Don't do event handling in HTML, do it in JavaScript
$("#Parent > div").on("click", function(event){
this.remove();
updateLabels();
});
function updateLabels(){
// Loop over each label in the child divs
$("#Parent > div > label").each(function(index){
$(this).text(index + 1); // Update the text based on the index
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Parent">
<div id="Child1">
<label>1</label>
</div>
<div id="Child2">
<label>2</label>
</div>
<div id="Child3">
<label>3</label>
</div>
<div id="Child4">
<label>4</label>
</div>
</div>
添加回答
舉報
