1 回答
TA贡献1856条经验 获得超5个赞
回答:
您的按钮的标识符为
states
,不scinfo
将 button 更改为
document.getElementById("states")
notdocument.getElementById("scinfo")
,因为该 ID不存在。您需要使用正确的索引。
您指向数组,
states.eachstate
但提供了查看第一项的零( [0] )索引。南卡罗来纳州信息位于第二项中,其索引为1。( [1] )
您需要提供
property
与数据相关的大小写正确的名称。这些是区分大小写的states.eachstate[1].population
是不一样的states.eachstate[1].Population
您需要提供正确的属性名称。
您使用
states.eachstate[0].bird
when 在您的数据中列为states.eachstate[0].StateBird
例子:
var button = document.getElementById("states");
var states = {
"eachstate": [
{
"Name":"North Carolina",
"Capital": "Raleigh",
"Population": "986,000",
"StateBird": "Who Cares"
},
{
"Name": "South Carolina",
"Capital": "Columbia",
"Population": "886,000",
"StateBird": "Hawk"
},
{
"Name": "Florida",
"Capital": "Tallahasee",
"Population": "975,000",
"StateBird": "Flamingo"
},
]
};
button.addEventListener("click", writestates, false);
function writestates()
{
document.getElementById("StateInfo").innerHTML = "<p>Name: " + states.eachstate[1].Name + "</p>" + "<p>" + "Capital: "
+ states.eachstate[1].Capital + "</p>" + "<p>" + "Bird: " + states.eachstate[1].StateBird + "</p>" + "<p>" + "Population: " +
states.eachstate[1].Population + "</p>"
}
<button id="states" type="button">SC Information</button>
<div class="showstate" >
<h1>
South Carolina
</h1>
<p id="StateInfo">
This is where the new information should show up!
</p>
</div>
添加回答
举报