更改图像边框颜色两次onclick

时间:2014-09-09 01:48:28

标签: javascript html image

我正在尝试更改图像边框颜色。首先,当用户点击图像时,图像边框颜色变为绿色,之后需要变为白色。以下是我的代码,如果我做错了,请告诉我。它始终显示白色图像边框颜色,从不绿色。

<div style="float:left;width:30%;">
 <img id="1" src="/images/37.jpg" width="163" height="100" alt="image not  displayed" onclick="openOnImageClick(this)" />
 <img id="2" src="/images/102.jpg" width="163" height="100" alt="image not displayed" onclick="openOnImageClick(this)" />
</div>

<script type="text/javascript">
function openOnImageClick(image)
{
 image.style.border = "3px solid green";

 // Some Ajax processing happens here

 image.style.border = "3px solid white"; 
}
</script>

1 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/tvf2mgje/

<div style="float:left;width:30%;">
     <img id="1" src="/images/37.jpg" width="163" height="100" alt="image not  displayed" onclick="openOnImageClick(this)" />
     <img id="2" src="/images/102.jpg" width="163" height="100" alt="image not displayed" onclick="openOnImageClick(this)" />
</div>

<script type="text/javascript">
    function openOnImageClick(image)
    {
        image.style.border = "3px solid green";

        // Some Ajax processing happens here

        setTimeout(function change ()
        {
            image.style.border = "3px solid white"
        }, 3000);
    }
</script>