1

我有一个戴着帽子的人的形象。我在此图像上有帽子的«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">
4

3 回答 3

2

你可以使用像这样简单的东西:

function hat(color) {
    document.getElementById("master").src = "images/" + color + ".png"; 
}
于 2012-08-10T22:09:05.697 回答
0

由于您已经传递了颜色的名称,因此您需要做的就是

  function hat(color) {
    document.getElementById("master").src = "images/" +color+".png";
}

更改图像。

于 2012-08-10T22:14:12.393 回答
0

这段代码不是很紧凑,但它会工作。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">
    };
于 2013-04-22T01:52:10.277 回答