在javascript创建的div标签中定位图像?

时间:2013-02-24 07:31:34

标签: javascript image html position

我正在尝试将图像放置在由javascript函数生成的div标记内。问题是图像的位置不是左边75像素而是顶部40像素,而是卡在0,0。这是代码。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<HTML lang="en">
<HEAD>  
<META http-equiv="Content-Type" content="text/html">    
</HEAD> 
<SCRIPT language = "JavaScript">
var my_div = null;
var newDiv = null; 
function creatediv(id, width, height, left, top, opacity) 
{ 
    var newdiv = document.createElement('div'); 
    newdiv.setAttribute('id', id);  

    newdiv.style.width =  width + "px";     
    newdiv.style.height = height + "px";     

    newdiv.style.position = "absolute";         
    newdiv.style.left = left + "px";         
    newdiv.style.top = top + "px";  

    newdiv.style.opacity = opacity;

    document.body.appendChild(newdiv); 

    my_div = document.getElementById(id);
    document.body.insertBefore(newdiv, my_div);
}
</SCRIPT>
<BODY onload="  creatediv('logo', 792, 1000, 75, 40, 1)">
<div  id = "logo" style=" z-index:1; font-size:200%; ">         
        <img src="some.gif" width="10%" height="10%">           
</div>
</BODY>
</html>

2 个答案:

答案 0 :(得分:1)

试试这个:Live Demo

function updateDiv(id, width, height, left, top, opacity) 
{ 
    var mydiv = document.getElementById(id);
    mydiv.setAttribute('id', id);  

    mydiv.style.width =  width + "px";     
    mydiv.style.height = height + "px";     

    mydiv.style.position = "absolute"; 
    mydiv.style.left = left + "px";         
    mydiv.style.top = top + "px";  

    mydiv.style.opacity = opacity;
}

<强>输出

<body onload="  updateDiv('logo', 792, 1000, 75, 40, 1)">
<div id="logo" style="z-index: 1; font-size: 200%; width: 792px; height: 1000px; position: absolute; left: 75px; top: 40px; opacity: 1;">         
        <img src="1.jpg" width="10%" height="10%">           
</div>  
</body>

<强>更新

如果您需要将图像文件作为参数传递:

<强> HTML

<BODY onload="  updateDiv('logo', 792, 1000, 75, 40, 1, '1.jpg')">
    <div  id = "logo" style=" z-index:1; font-size:200%; "></div>
</BODY>

<强>的Javascript

function updateDiv(id, width, height, left, top, opacity, imgSrc) 
{ 
    var mydiv = document.getElementById(id);
    mydiv.setAttribute('id', id);  

    mydiv.style.width =  width + "px";     
    mydiv.style.height = height + "px";     

    mydiv.style.position = "absolute"; 
    mydiv.style.left = left + "px";         
    mydiv.style.top = top + "px";  

    mydiv.style.opacity = opacity;

    mydiv.innerHTML = '<img src="' + imgSrc + '" width="10%" height="10%">';
}

答案 1 :(得分:1)

如果你想让图像在javascript生成的div中,那么你可以使用这段代码:

function creatediv(id, width, height, left, top, opacity, imageurl) 
{ 
    var newdiv = document.createElement('div'); 
    newdiv.setAttribute('id', id);  

    newdiv.style.width =  width + "px";     
    newdiv.style.height = height + "px";     

    newdiv.style.position = "absolute";         
    newdiv.style.left = left + "px";         
    newdiv.style.top = top + "px";  

    newdiv.style.opacity = opacity;

    newdiv.innerHTML='<img src="' + imageurl + '" width="10%" height="10%">';

    document.body.appendChild(newdiv); 

    my_div = document.getElementById(id);
    document.body.insertBefore(newdiv, my_div);
}