Adds the provided styles to the given HTML element.
- Use
Object.assign()
andHTMLElement.style
to merge the providedstyles
object into the style of the given element.
代码实现
const addStyles = (el, styles) => Object.assign(el.style, styles);
addStyles(document.getElementById('my-element'), {
background: 'red',
color: '#ffff00',
fontSize: '3rem'
});
翻译自:https://www.30secondsofcode.org/js/s/add-styles-to-html-element