如何使用JavaScript将类添加到HTMLcollection的classList中

时间:2017-03-31 15:54:29

标签: javascript jquery html d3.js

我在尝试将类添加到HTMLcollection时遇到了麻烦。我正在做的是在点击时填充地图,然后在其输入/选择框中填充相应的县,州和其他人。我想采取正确的县和州,然后添加一个新的县到县。但我似乎无法弄清楚如何让它发挥作用。

这是我的代码:

            $("#locationStateSelect").val(editedTd5);
            $("#locationCountySelect").append($("<option>", {
                name: "locationCountyName",
                value: editedTd4,
                text: editedTd4
            }));

            countySelect = editedTd4;
            stateSelect = editedTd5;

            stateNameOne = "";
            //get state info
            for (i=0;i<states.length;i++) {
                if(states['StateName'][i] == stateSelect) {
                    stateName = states["StateName"][i];
                    stateIdClass = states['StateId'][i];
                    stateNameOne = stateName.replace(/([A-Z])\s(?=[A-Z])/g, '$1');
                }
            }
            countyNameOne = "";
            //get county info and highlight the county
            for(i=0;i<counties.length;i++) {
                if(counties["CountyName"][i] == countySelect && counties['StateId'][i] == stateIdClass) {
                    countyId = counties["CountyId"][i];
                    countyName = counties["CountyName"][i];
                    countyNameOne = countyName.replace(/([A-Z])\s(?=[A-Z])/g, '$1');
                }
            }
            var elCounty = document.getElementsByClassName(countyNameOne +" "+ stateNameOne);               
            console.log(elCounty.item(0));

            //fill the correct county
            for(i=0;i<elCounty.length;i++) {
                countyList = elCounty[i];
                console.log(countyList);
                if(countyList.classList.contains(countyNameOne) && countyList.classList.contains(stateNameOne)) {
                    console.log("true");
                    countyList.classList.add("selectedCounty");
                }
            }

我一直收到一个错误,说不能包含未定义的内容。我正在使用d3地图,如果这有助于更好地构建它。我需要做的就是以某种方式向HTMLcollection添加一个新类。

0 个答案:

没有答案