我想分配样式给一个对象。最初的代码是es6对象解构不起作用
targetEl.style.top = `${top}px` ;
targetEl.style.display = 'block';
targetEl.style.background = `url(${this.props.imgSrc}) no-repeat`;
targetEl.style.backgroundSize = "1800px 900px";
我试图用ES6解构和改写这样的代码:
targetEl.style = {...targetEl.style,
top:`${top}px`,
display: 'block',
background: `url(${this.props.imgSrc}) no-repeat`,
backgroundSize: "1800px 900px" };
但由于某种原因,它似乎并没有工作。我究竟做错了什么?
怀疑你可以覆盖整体e风格对象。你试图解决什么更高水平的问题? – charlietfl
.style是只读的。也许'Object.assign(targetEl.style,{top:'$ {top} px'})'。此外,它的类型不是对象 – Slai
它不是对象解构。 –