首页 > 解决方案 > 如何在 js 中使用 SHIFT() 方法?

问题描述

我尝试使用 shift() 方法删除第一个元素。但它没有用。如何使用来更正此功能?

     `// id="delete-first" in HTML
     // the element into the array displayed in HTML 
     // I want to delete the first element
    $("#delete-first").click(function(){ 
        $("#list-student").shift(); // I have issue this line
        // $("list-student").first().remove(); // this code is not working too.
    });`

Please, fix it to help me. 
Sincerely.

标签: javascriptjqueryhtml

解决方案


您正在尝试删除该#list-student项目的第一次出现 - 只需选择它并使用remove

$("#list-student").remove();

您不需要选择第一个元素,因为 ID 是唯一的。


推荐阅读