这是html
<ul id="List">
<li><h2>Numeros Primos</h2></li>
<li>Hello World</li>
<li>Not Work</li>
</ul>
<p id="count"></p>
这是JavaScript
class List {
static Restart() {
document.getElementById("List").innerHTML = "<li><h2>Numeros Primos</h2></li>";
}
static add(Number) {
document.getElementById("List").innerHTML += "<li>" + Number + "</li>"
}
get count() {
return document.getElementById("List").childElementCount
}
}
document.getElementById("count").innerHTML = List.count;
// return undefined
问题
我无法在列表中获得计数
返回“未定义”
答案 0 :(得分:2)
使计数静态:)
class List {
static Restart() {
document.getElementById("List").innerHTML = "<li><h2>Numeros Primos</h2></li>";
}
static add(Number) {
document.getElementById("List").innerHTML = document.getElementById("List").innerHTML + "<li>" + Number + "</li>"
}
static get count() {
return document.getElementById("List").childElementCount;
}
}
document.getElementById("count").innerHTML = List.count;
<ul id="List">
<li><h2>Numeros Primos</h2></li>
<li>Hello World</li>
<li>Not Work</li>
</ul>
<p id="count"></p>