我一直在使用这段代码:
<div class="divClassName">
<button onClick="functionName1();");'>Enter Text Here</button>
<script>
function functionName1() {
var src = "name.jpg";
show_image("name.jpg", 200, 200, "absolute", 1, "<alt>");
}
function show_image(src, width, height, position, zIndex, alt) {
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height
img.position = position;
img.style.zIndex = zIndex;
img.alt = alt;
document.body.appendChild(img);
}
</script>
</div>
<!-- Use actual names for divClassName, functionName1, and name.jpg if you want to. -->
这使得图像在单击按钮时出现,但它出现在屏幕的左上角,而不是我想要的位置。我一直在努力
function show_image(src, width, height, position, left, top, zIndex, alt) {
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height
img.position = position;
img.left = left;
img.top = top;
img.style.zIndex = zIndex;
img.alt = alt;
……但这不起作用。有任何修复/答案吗?
编辑:问题得到解答。用过的:
img.style.position = position;
img.style.left = left;
img.style.top = top;