javascript - 从对象中删除值

标签 javascript

我有一个对象,我试图从传递给元素的 cssProperty 中删除该对象。我尝试了一些方法,但没有成功,我很茫然。

该对象看起来像

Object {background-color: "#91eae8"}

我尝试像这样删除它

delete this.cssProps.background-color;

delete this.cssProps[background-color];

delete this.cssProps['background-color'];

两者都不起作用并引发 viewModel 错误,因为它不知道我在做什么。我无法更改对象的引入方式以使其周围带有引号或类似内容。

最佳答案

在 css 中不存在未定义属性之类的东西。它们都有默认值。因此,尝试删除属性是行不通的。相反,您可以将该值更改为不同的值以获得您想要的行为。

我会尝试做其中之一

this.style.backgroundColor = "inherit";//background will be the same as it's parent
this.style.backgroundColor = "transparent";//background will show what is behind it
this.style.backgroundColor = "inital"; //sets it to the default value

关于javascript - 从对象中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217506/

相关文章:

javascript - jQuery 菜单按钮仅在 webkit 中的下拉菜单中消失,如何让它保持原状?

javascript - 如果 ID 部分匹配,则选择一个元素

javascript - 在 Backbone 应用程序中重新加载主干 View 和 i18n 翻译文件?

javascript - 设计 html 编辑器预览功能背后的理论

javascript - 2页之间实时同步

javascript - 如何使用 phantomjs 和 php-webdriver 跟踪页面重定向并持续设置常量浏览器用户代理

javascript - 使用 Node.js 和 JavaScript 从缓冲区渲染 PDF

javascript - Node.js 网络抓取,在 URL 数组上循环

为所有先前实例触发的实例设置的 Javascript 类事件

javascript:分割字符串并替换值