Javascript 将图像放置在另一个图像之上

标签 javascript image

我有以下源代码,我想要做的是如果输入特定代码则显示图像。诀窍是我需要将图像放在另一个图像之上。有办法做到这一点吗?

var roomCode = prompt("Please enter the room code", "Enter room code here");
        if(roomCode==='102')
        {
            /*I want the image to appear at certain coordinates (on top of another image).*/
        }

最佳答案

只需将先前图像的 src 属性替换为新图像的路径即可。

示例:

HTML

<img id="replacable-img" src="http://www.wallpaperfunda.com/wp-content/uploads/2014/03/images-2.jpg" width="400px" alt="Original image">
<button id="change">Next Image</button> // Just example trigger

Javascript

var img = document.getElementById('replacable-img');
var button = document.getElementById('change');

change.onclick = function(e) {
  img.setAttribute('src', 'http://www.wired.com/images_blogs/wiredscience/2012/10/kitten-kisses-dog.jpg')
}

只需在每个 if block 中使用 element.setAttribute('src', 'PATH') :)

这是一个示例:http://jsbin.com/cefutaqo/1/

关于Javascript 将图像放置在另一个图像之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23355084/

相关文章:

javascript - FF 和 Chrome 标签的 CSS 行为项

javascript - trim 小数点前的数字

javascript - XDomainRequest 有时会随机中止

image - RGB 图像的 Matlab 低通滤波器

php - 无法使用 PHP 的 imagerotate 旋转图像

c - 如何使用 C 从 ImageMagick Wand 中提取 PPM 图像属性?

objective-c - UIImage drawInRect : is very slow; is there a faster way?

javascript - document.getElementById().style.display = 'block' ;不工作

javascript - 观察简单数组的元素

html - 为什么 div 可以正确拉伸(stretch),但图像却不能?