未捕获的类型错误:无法读取未定义的属性(读取“remove”)
2022-01-15
14943
我的代码有什么问题?
Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
和
Uncaught TypeError: Cannot read properties of undefined (reading 'add')
document.querySelector("#elastic").oninput = function () {
let val = this.value.trim();
let elasticItem = Array.from(document.querySelectorAll(".elastic li"));
if (val != "") {
elasticItem.forEach(function (elem) {
if (elem.innerText.search(val) === -1) {
elem.classlist.add("hide");
} else {
elem.classlist.remove("hide");
}
});
}
};
.hide {
display: none;
}
<div>
<input type="text" id="elastic" placeholder="Search" />
</div>
<div>
<ul class="elastic">
<li>ht</li>
<li>sdf</li>
<li>qwe</li>
<li>cxv</li>
<li>sad</li>
<li>sdf</li>
<li>dfg</li>
</ul>
</div>
1个回答
JavaScript 区分大小写
将
classlist
更改为
classList
(
l
→
L
)
elem.classList.add('hide');
elem.classList.remove('hide');
Nice18
2022-01-15