Опираясь на превосходный ответ @ Premasagar; если вы не хотите удалять все другие встроенные стили, используйте эту
//accepts the hyphenated versions (i.e. not 'cssFloat')
addStyle(element, property, value, important) {
//remove previously defined property
if (element.style.setProperty)
element.style.setProperty(property, '');
else
element.style.setAttribute(property, '');
//insert the new style with all the old rules
element.setAttribute('style', element.style.cssText +
property + ':' + value + ((important) ? ' !important' : '') + ';');
}
Невозможно использовать removeProperty()
, поскольку оно не удаляет правила !important
в Chrome.
Не могу использовать element.style[property] = ''
, потому что он принимает только camelCase в FireFox.