0

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

<!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>
4

1 回答 1

1

您可以将代码修改为

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

to

<body onload=" creatediv('xdiv', 'html', 100, 100, 10, 10) ">
于 2013-02-24T04:49:30.310 回答