在Javascript中创建的DIV没有显示onload?

时间:2013-02-24 04:33:10

标签: javascript html onload

由于某种原因,我无法获得以下代码来生成我在Javascript代码中创建的DIV。我很确定它与creatediv('xdiv', html, 100, 100, 10, 10) id = 'xdiv'中的函数参数id有关。

<!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 个答案:

答案 0 :(得分:1)

您可以将代码修改为

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

to

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