我有一个戴着帽子的人的形象。我有帽子的«master.png»图像(本身)在这个图像上,这将改变为一个不同的图像(相同的帽子/不同的颜色),每次用户点击不同的颜色样本下面。
javascript函数可以工作,但我无法将颜色变量值分配给我的色板。因此,无论您单击哪个样例,都只显示列出的最后一个js src图像。我也不确定我的javascript变量代码是否正确。
我还没能在网上找到任何关于如何做到这一点的东西。请帮帮我!
代码如下:
<script type="text/javascript">
function hat(color)
{
var color = ['redhat', 'bluehat', 'blackhat'];
document.getElementById("master").src="images/redhat.png";
document.getElementById("master").src="images/bluehat.png";
document.getElementById("master").src="images/blackhat.png";
}
</script>
<img id="hat" src="images/manwearinghat.png" width="100%" class="center">
<img id="master" src="images/masterhat.png" width="100%" alt=" " name="master">
<img src="images/swatchs/redswatch.png" width="69" height="69" onclick="hat('redhat');" value="redhat" style="z-index:3">
<img src="images/swatchs/blueswatch.png" width="69" height="69" onclick="hat('bluehat');" value="bluehat" style="z-index:3">
<img src="images/swatchs/blackhat.png" width="69" height="69
" onclick="hat('blackhat');" style="z-index:3">发布于 2012-08-11 06:09:06
你可以使用像这样简单的东西:
function hat(color) {
document.getElementById("master").src = "images/" + color + ".png";
}发布于 2012-08-11 06:14:12
因为您已经传递了颜色的名称,所以您需要做的就是
function hat(color) {
document.getElementById("master").src = "images/" +color+".png";
}来更改图像。
发布于 2013-04-22 09:52:10
这段代码不是很紧凑,但它可以工作。HTML:
<img id="hat" src="images/manwearinghat.png" width="100%" class="center">
<div id="master"></div>
<div>
<img src="images/swatchs/redswatch.png" width="69" height="69" onclick="hat('redhat');"
value="redhat" style="z-index:3">
<img src="images/swatchs/blueswatch.png" width="69" height="69" onclick="hat('bluehat');"
value="bluehat" style="z-index:3">
<img src="images/swatchs/blackhat.png" width="69" height="69
" onclick="hat('blackhat');" style="z-index:3">
</div>Javascript:
function hat(color){
if (color == 'bluehat'){
document.getElementById('picture').innerHTML=<img id="master"
src="images/bluehat.png" width="100%" alt=" " name="master">
if (color == 'blackhat'){
document.getElementById('picture').innerHTML=<img id="master"
src="images/blackhat.png" width="100%" alt=" " name="master">
if (color == 'redhat'){
document.getElementById('picture').innerHTML=<img id="master" src="images/redhat.png"
width="100%" alt=" " name="master">
};https://stackoverflow.com/questions/11909883
复制相似问题