Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
清理javascript dom脚本?_Javascript_Html_Dom - Fatal编程技术网

清理javascript 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

嗨,创建dom元素有更好或更干净的方法吗?像是在数组中还是什么

示例代码:

$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',
});