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>
添加回答
举报