2013-02-24 2 views
0

어떤 이유인지 나는 Javascript 코드 안에 생성 한 DIV를 생성하기 위해 다음 코드를 얻을 수 없습니다. 나는 그것이 인 creatediv('xdiv', html, 100, 100, 10, 10)의 function 매개 변수 id와 관련이 있다고 확신합니다. 당신이 할 수있는Javascript에서 생성 된 DIV가 onload를 표시하지 않습니까?

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<script> 
var my_div = null; 
var newDiv = null; 
function creatediv(id, html, width, height, left, top) 
{ 
    var newdiv = document.createElement('div'); 
    newdiv.setAttribute('id', id); 
    if (width) 
    { 
     newdiv.style.width = 300; 
    } 
    if (height) 
    { 
     newdiv.style.height = 300; 
    } 
    if ((left || top) || (left && top)) 
    { 
     newdiv.style.position = "absolute"; 

     if (left) 
     { 
      newdiv.style.left = left; 
     } 
     if (top) 
     { 
      newdiv.style.top = top; 
     } 
    } 

    newdiv.style.background = "#00C"; 
    newdiv.style.border = "4px solid #000"; 

    if (html) 
    { 
     newdiv.innerHTML = html; 
    } 
    else 
    { 
     newdiv.innerHTML = "nothing"; 
    } 
    document.body.appendChild(newdiv); 

    my_div = document.getElementById(id); 
    document.body.insertBefore(newdiv, my_div); 
} 
</script> 
<body onload=" creatediv('xdiv', html, 100, 100, 10, 10) "> 
<div id='xdiv'> c</div> 
</body> 
</html> 

답변

1

<body onload=" creatediv('xdiv', html, 100, 100, 10, 10) "> 

to 

<body onload=" creatediv('xdiv', 'html', 100, 100, 10, 10) "> 
같은 코드를 수정
관련 문제