我有以下代码,我想将两个 DIV 标签的不透明度更改为 0.5。我似乎无法让它像宽度和高度一样工作。我没有正确传递不透明度参数还是这newdiv.style.opacity = opacity;
是不正确的部分?
<!DOCTYPE html>
<html>
<head>
</head>
<script>
var my_div = null;
var newDiv = null;
function creatediv(id, html, 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.background = "#0CC";
newdiv.style.border = "10px solid #000";
newdiv.style.opacity = opacity;
newdiv.innerHTML = 'html n';
document.body.appendChild(newdiv);
my_div = document.getElementById(id);
document.body.insertBefore(newdiv, my_div);
}
</script>
<body onload=" creatediv('xdiv1', 300, 300, 100, 100, .5);creatediv('xdiv2', 30, 30, 100, 100, .5)">
<div id='xdiv1'> </div>
<div id='xdiv2'> </div>
</body>
</html>