admin管理员组文章数量:1336281
What's the problem in my code?
Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
and
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>
What's the problem in my code?
Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
and
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>
Share
edited Jan 16, 2022 at 19:22
Nice18
5662 silver badges12 bronze badges
asked Jan 15, 2022 at 6:35
AKAENOAKAENO
431 gold badge1 silver badge3 bronze badges
2
-
2
just change
classlist
toclassList
– Saeed Shamloo Commented Jan 15, 2022 at 6:37 -
You can use hidden attribute instead.
elem.hidden = true
for hiding an element andelem.hidden = false
for showing an element. – Nice18 Commented Jan 15, 2022 at 7:11
1 Answer
Reset to default 3JavaScript is case-sensitive
Changeclasslist
to classList
(l
→ L
)
elem.classList.add('hide');
elem.classList.remove('hide');
本文标签: javascriptUncaught TypeError Cannot read properties of undefined (reading 39remove39)Stack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read properties of undefined (reading 'remove') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406220a2468873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论