清理javascript dom脚本?
嗨,创建dom元素有更好或更干净的方法吗?像是在数组中还是什么 示例代码:清理javascript dom脚本?,javascript,html,dom,Javascript,Html,Dom,嗨,创建dom元素有更好或更干净的方法吗?像是在数组中还是什么 示例代码: $hubbe.toolbarLeft = document.createElement('DIV'); $hubbe.toolbarLeft.setAttribute('id', 'toolbar-left'); $hubbe.toolbarLeft.style.width = '50px'; $hubbe.toolbarLeft.style.height = '100%'; $hubbe.toolbarLeft.sty
$hubbe.toolbarLeft = document.createElement('DIV');
$hubbe.toolbarLeft.setAttribute('id', 'toolbar-left');
$hubbe.toolbarLeft.style.width = '50px';
$hubbe.toolbarLeft.style.height = '100%';
$hubbe.toolbarLeft.style.position = 'fixed';
$hubbe.toolbarLeft.style.left = '0';
$hubbe.toolbarLeft.style.backgroundColor = 'grey';
$hubbe.toolbarLeft.style.color = 'white';
$hubbe.toolbarLeft.style.textAlign = 'center';
document.body.appendChild($hubbe.toolbarLeft);`
有一些库和框架可以帮助您实现这一点,比如jQuery。如果您坚持使用普通JavaScript,可以通过Object.assign批量设置属性来简化一点:
非常感谢。object.assign正是我想要的:
Object.assign($hubbe.toolbarLeft.style, {
width: '50px',
height: '100%',
position: 'fixed',
left: '0',
backgroundColor: 'grey',
color: 'white',
textAlign: 'center',
});