TypeError Cannot set property scrollHeight of
2024年1月25日大约 1 分钟约 383 字
function _scrollBar(html, focusIndex, keyCode) {
console.log("lx3 test _scrollBar",
"\nhtml.scrollHeight 1= ", html.scrollHeight,
"\nhtml focusIndex = focusIndex", html.length,
"\nhtml.length =")
if(focusIndex === 0 && keyCode === key.UP) {
html.scrollHeight = 2000
return
}
if(focusIndex === (_switchHtmlElementToArray(html).length - 1) && keyCode === key.DOWN) {
html.scrollHeight = 0
return
}
html.scrollHeight = keyCode === key.UP && focusIndex >= 5
<!-- more -->
? html.scrollHeight - 52
: html.scrollHeight + 52
}TypeError: Cannot set property scrollHeight of #<Element> which has only a getter方案一:(解决的是undefined的问题,和此问题无关)
这个DOM元素找不到,或者是初始化的时候没有初始化成功,总之就是这个DOM元素并没有创建成功就进行了操作.保证这个DOM元素创建成功了就不会存在这个问题了.判断当前DOM元素是否创建成功再进行进行操作就得以解决。
方案二:
原因:
该错误提示出现于试图给一个仅仅设置了 getter 特性的属性赋新值的时候。在非严格模式下会被静默忽略,而在严格模式下会报 TypeError 错误。
方案:
至于修复问题的方法,可以将试图赋值的代码移除,或者是为它添加一个 setter 特性
方案三: 以往使用ref的时候获取了HTML元素是可以进行赋值的,但是使用torefs解构出去之后就没法赋值了,可能是和这个相关
