简体   繁体   English

如何使dom操作更清晰

[英]how to make dom manipulation more clear

Have some code like: 有一些代码如:

var el = document.createElementNS('http://www.w3.org/2000/svg', 'rect');

var c = this.chart;

el.setAttribute('x', c.x + c.padding + 'px');
el.setAttribute('y', c.y + c.padding + i * (b.height + b.margin) + 'px');
el.setAttribute('width', model.value * c.scale + 'px');
el.setAttribute('height', b.height + 'px');
el.classList.add('bar');

this.svg.appendChild(el);

Any options to make it look not as ugly? 有什么选择让它看起来不那么难看? There is a chance that can be more attributes... 有可能有更多的属性......

Time to use a function? 是时候使用一个功能? Something like: 就像是:

function setAttributes(el, attrs) {
  for(var key in attrs) {
    el.setAttribute(key, attrs[key]);
  }
}

And call it as: 称之为:

setAttributes(elem, {"src": "http://example.com/something.jpeg", "height": "100%", ...});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM