interface StyleObj { [ key ] : string ; } function _isStyleObjEmpty ( styleObj ) { return Object . keys ( styleObj ) . length === 0 ; } example param styleObj export function stringifyStyleObj ( styleObj ) { return Object . keys ( styleObj ) . reduce ( ( str , name ) => str + `${ name }:${ styleObj [ name ] };` , '' ) ; } example param styleStr export function objectifyStyleStr ( styleStr ) { const styleObj = { } ; const cssStyles = styleStr . split ( ';' ) ; cssStyles . forEach ( ( css ) => { const [ name , value ] = css . split ( ':' ) ; if ( name && value ) { styleObj [ name ] = value ; } } ) ; return styleObj ; } param element param styleObj export function updateDomStyles ( element , styleObj ) { const styleStr = element . getAttribute ( 'style' ) || '' ; const _styleObj = { ... objectifyStyleStr ( styleStr ) , ... styleObj , } ; if ( ! _isStyleObjEmpty ( _styleObj ) ) { element . setAttribute ( 'style' , stringifyStyleObj ( _styleObj ) ) ; } } param element param styleName export function removeDomStyle ( element , styleName ) { const styleStr = element . getAttribute ( 'style' ) || '' ; const styleObj = objectifyStyleStr ( styleStr ) ; if ( styleObj [ styleName ] ) { delete styleObj [ styleName ] ; } if ( _isStyleObjEmpty ( styleObj ) ) { element . removeAttribute ( 'style' ) ; } else { element . setAttribute ( 'style' , stringifyStyleObj ( styleObj ) ) ; } }