gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read properties of undefined (reading 'remove' )

转载 作者:行者123 更新时间:2023-12-05 04:37:17 26 4
gpt4 key购买 nike

我的代码有什么问题?

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>

最佳答案

JavaScript 区分大小写


classlist更改为classList (lL)

elem.classList.add('hide');
elem.classList.remove('hide');

关于javascript - 未捕获的类型错误 : Cannot read properties of undefined (reading 'remove' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70719360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com