【发布时间】:2021-05-02 08:18:45
【问题描述】:
我使用 createElement() 创建了一个列表元素的 nodeList。然后我使用 Array.from() 将有问题的 nodeList 转换为我可以迭代的数组。我想根据索引的值应用不同的宽度。如果索引是 300px 的偶数宽度,则为 500px 的宽度。但是,控制台返回“Cannot read property 'style' of undefined at bottleOnTheWall”。
我还使用 [...] 将我的 nodeList 转换为数组,但也没有成功。我的猜测是,它首先不是 nodeList,这意味着它不能转换为数组。至少不使用这两种方法中的任何一种。
我想知道是否有人可以指出我出错的地方并修复我的代码。我一直在尝试这样做,这对健康有益。
const bottles = document.getElementById("bottles");
count = 99;
var myArray = [];
var j = 0;
function bottlesOnTheWall() {
while (count > 0) {
if(count > 2) {
myArray.push(`${count} bottles of beer on the wall, ${count} bottles of beers. Take one down and pass it around, ${count - 1} bottles of beer on the wall`)
} else if (count === 2) {
myArray.push(`${count} bottles of beer on the wall, ${count} bottles of beers. Take one down and pass it around, ${count - 1}bottle of beer on the wall`)
} else if (count === 1) {
myArray.push(`${count} bottle of beer on the wall, ${count} bottles of beers. No more bottle of beer on the wall`)
} else {
myArray.push(`No more bottles of beer on the wall. No more bottles of beer. Go to the shop and buy some ${count} more.`)
}
count--
}
while (j < myArray.length) {
var liElement = document.createElement("li");
var text = document.createTextNode(myArray[j]);
liElement.appendChild(text);
bottles.appendChild(liElement);
var bottlesArray = Array.from(bottles);
if(j % 2) {
bottlesArray[j].style.width = "300px";
} else {
bottlesArray[j].style.width = "500px";
}
j++;
}
}
bottlesOnTheWall();
#bottles {
line-height: 2;
letter-spacing: 3px;
}
/* ul {
list-style-image: url('beer.png');
} */
body {
background: #FFF8DC;
}
li {
max-width: 500px;
margin: auto;
margin-bottom: 10px;
}
ul li {
background: #FFEBCD;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>bottles on the wall</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<ul id="bottles" ></ul>
<script src="index.js" type="text/javascript"></script>
</body>
</html>
【问题讨论】:
-
const bottles = document.getElementById("bottles");返回一个节点,而不是节点列表。Array.from(bottles)因此尝试将节点转换为数组。 -
另外,我不太确定为什么要转换为数组
bottlesArray[j].style.width = "some width";意味着您要转换为数组只是为了从中获取最后一个索引j。如果您想要最后一个附加元素,它可能只是liElement.style.width = "some width";。 -
还有一个问题,为什么要先创建一个数组,然后再迭代呢?
标签: javascript html arrays nodelist