This is the html
<ul id="List">
<li><h2>Numeros Primos</h2></li>
<li>Hello World</li>
<li>Not Work</li>
</ul>
<p id="count"></p>
This is the 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
PROBLEM
I can't get count number in the list
it return "undefined"
最佳答案
使计数静态 :)
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>
关于javascript - 在javascript类中获取文档计数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52905506/