Вы всегда можете написать функции-оболочки вокруг неуклюжего DOM API, чтобы сделать код более читабельным.Я обычно использую что-то вроде:
function newElement (type,attr,children) {
var el = document.createElement(type);
for (var n in attr) {
if (n == 'style') {
setStyle(el,attr[n]); /* implementation of this function
* left as exercise for the reader
*/
}
else {
el[n] = attr[n];
}
}
if (children) {
for (var i=0; i<children.length; i++) {
el.appendChild(children[i]);
}
}
}
function newText (text) {
document.createTextNode(text);
}
Тогда вы можете написать гораздо более декларативный код:
var tr = newElement('tr',{},[
newElement('td',{},[
newText('some value')
])
]);
Просто запомните различия между css и javascript:
var div = newElement('div',{
className:'foo',
style:{
marginLeft:'10px'
}
},[
newText('notice we use "className" instead of class" '+
'and "marginLeft" instead of "margin-left"')
]);