如何修复“无法设置未定义的属性”?

问题描述

用户按下按钮时,我试图为我的可编辑div框设置背景色,但是当按下按钮时,除背景色之外的所有内容都会改变:

shiftKey: boolean

我得到了错误

TypeError:无法设置未定义的属性'backgroundColor'。

我一直在网上寻找解决方案,甚至与此问题相关的类似问题,但它们并没有真正帮助我寻找所需的内容。我究竟做错了什么?为什么控制台将其显示为未定义?

我什至做到了:

function saveBio() {

 let biography = document.getElementById("bio-content").innerHTML;
  biography = localStorage.setItem('bio',biography);

 let content = document.getElementById("bio-content").setAttribute("contenteditable",false).backgroundColor = "green";`
}

尽管风格不属于那里。

控制台返回: TypeError:无法读取未定义的属性“样式”。

我该如何解决

解决方法

table

这里的问题是:

let content = document.getElementById("bio-content").setAttribute("contenteditable",false).backgroundColor = "green";`

将返回document.getElementById("bio-content").setAttribute("contenteditable",false) ,以防止您链接其他呼叫。

您可以保存元素引用,然后进行更改:

undefined