如何使用JavaScript将类添加到HTMLcollection的类列表中
我在尝试向HTMLcollection添加类时遇到问题。我所做的是在单击时填充地图,然后在其输入/选择框中填充相应的县、州和其他。我想选择正确的县和州,然后向县添加一个新类。但我似乎不知道该怎么做 这是我的密码:如何使用JavaScript将类添加到HTMLcollection的类列表中,javascript,jquery,html,d3.js,Javascript,Jquery,Html,D3.js,我在尝试向HTMLcollection添加类时遇到问题。我所做的是在单击时填充地图,然后在其输入/选择框中填充相应的县、州和其他。我想选择正确的县和州,然后向县添加一个新类。但我似乎不知道该怎么做 这是我的密码: $("#locationStateSelect").val(editedTd5); $("#locationCountySelect").append($("<option>", { name:
$("#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");
}
}
$(“#locationStateSelect”).val(EditedD5);
$(“#locationCountySelect”)。附加($(“”{
名称:“locationCountyName”,
值:EditedD4,
文本:EditedD4
}));
countySelect=editedTd4;
stateSelect=editedTd5;
stateNameOne=“”;
//获取状态信息
对于(i=0;i在最后的if
语句中,您检查的是elcountylist
,而不是countyList
,您没有使用countyList
,您只是在定义它。您试图访问elcountry.classList
,但elcountry
是一个节点列表。elcountry.classList
因此未定义ed.I's console.logged the elCounty,它肯定是一个HTMLcollection。更改代码后,我仍然会得到相同的错误。你能显示html代码吗?另一件事是文档。getElementsByClassName
我想真的返回了一个div标记集合吗?在你的if
语句的末尾,你要检查elCounty
而不是countyList
您没有使用countyList
,您只是在定义它。您试图访问elCounty.classList
,但是elCounty
是一个节点列表。elCounty.classList
因此未定义。我已经将elCounty代码我仍然得到相同的错误。你能显示html代码吗?另一件事是document.getElementsByClassName
我想真的返回了一组div标记吗?