<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>星期1</li>
        <li>星期2</li>
        <li>星期3</li>
        <li>星期4</li>
        <li>星期5</li>
    </ul>
    <script>
        var lis = document.querySelectorAll('li');
        var ul = document.querySelector('ul')
        for (var i = 0; i < lis.length; i++) {
            //添加自定义属性index
            
            lis[i].index = i;
            
            lis[i].onclick = function() {
                ul.removeChild(this);
                console.log(this.index);//打印当前li的index
            }
        }
    </script>
</body>
</html>


本文转载:CSDN博客