每日鸡汤:如果你半夜醒来发现自己已经好长时间没读书,而且没有任何负罪感的时候,你就必须知道,你已经堕落了。不是说书本本身特了不起,而是读书这种行为意味着你没有完全认同于这个现世和现实,你还有追求,还在奋斗,你还有不满,你还在寻找另一种可能性,另一种生活方式。——陈平原。

    function chooseColor() {
        var colorLi = document.getElementById('page2').getElementsByTagName('li'),
            colorValue = document.getElementById('choose-color-value'),
            choosedArray = [];

        /*
        *    判断当前点击元素是否选中 选中添加class='active-border' 同时向数组添加对应的(index+1)值
        *    二次点击时取消选中删除class='active-border' 同时删除选中时对应的(index+1)值
        */

        for (var i = 0; i < colorLi.length; i++) {
            //解决闭包问题
            colorLi[i].i = i;         
            colorLi[i].index = i+1;
            colorLi[i].onclick = function () {
            var choosedStyle = this.getElementsByClassName('choosed')[0].classList.contains("active-border");
            if (choosedStyle) {
                this.getElementsByClassName('choosed')[0].className = "choosed";
                                // 判断自身 存在即删除
                if( choosedArray.indexOf(this.index === -1) ){
                    choosedArray.splice(choosedArray.indexOf(this.index),1);
                }
            } else {
                this.getElementsByClassName('choosed')[0].className += " active-border";
                choosedArray.push(this.index);
            }
            colorValue.value = choosedArray;
            }
        }
    }

 
判断数组添加、删除主要用到以下部分:

    if( choosedArray.indexOf(this.index === -1) ){
        hoosedArray.splice(choosedArray.indexOf(this.index),1);
    }

        
    if (this.bedroomNum.indexOf(id) === -1) {
        this.bedroomNum.push(id)
    } else {
        this.bedroomNum.splice(this.bedroomNum.indexOf(id), 1)
    }