Sử dụng quy trình tương tự. Bạn đã có biến iDivvẫn đề cập đến yếu tố ban đầu <div id='block'>bạn đã tạo. Bạn chỉ cần tạo một cái khác <div>và gọi appendChild().
// Your existing code unmodified...
var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';
document.getElementsByTagName('body')[0].appendChild(iDiv);
// Now create and append to iDiv
var innerDiv = document.createElement('div');
innerDiv.className = 'block-2';
// The variable iDiv is still good... Just append to it.
iDiv.appendChild(innerDiv);
http://jsfiddle.net/W4Sup/1/
Thứ tự tạo sự kiện không nhất thiết phải như tôi có ở trên. Bạn có thể xen kẽ cái mới innerDivvào div bên ngoài trước khi bạn thêm cả hai vào <body>.
var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';
// Create the inner div before appending to the body
var innerDiv = document.createElement('div');
innerDiv.className = 'block-2';
// The variable iDiv is still good... Just append to it.
iDiv.appendChild(innerDiv);
// Then append the whole thing onto the body
document.getElementsByTagName('body')[0].appendChild(iDiv);
document.body.