javascript - 该脚本一次后不会更改图像

标签 javascript html

该脚本仅更改图像一次。那么它根本就不起作用。

脚本:

<script type="text/javascript">
    function change_image(){
        if (window.document.pic.src='images/NAV_OFFhome_trans.png')
        {
            window.document.pic.src='images/NAV_OFFhist_trans.png';
        }
        else if (window.document.pic.src='images/NAV_OFFhist_trans.png')
        {
            window.document.pic.src='images/NAV_OFFhome_trans.png';
        }
    }
</script>

HTML:

<div id="main" >
<img src="images/NAV_OFFhome_trans.png" name="pic" onclick="change_image()"     style="position:absolute;"/>
</div>

最佳答案

在 if/else if 语句中,您设置 src 而不是检查值。确保使用 == 而不是 =

因此:

<script type="text/javascript">
    function change_image(){
        if (window.document.pic.src == 'images/NAV_OFFhome_trans.png')
        {
            window.document.pic.src = 'images/NAV_OFFhist_trans.png';
        }
        else if (window.document.pic.src == 'images/NAV_OFFhist_trans.png')
        {
            window.document.pic.src = 'images/NAV_OFFhome_trans.png';
        }
    }
</script>

以下是一个良好衡量标准的示例:http://jsfiddle.net/rd6dL/

关于javascript - 该脚本一次后不会更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735334/

相关文章:

javascript - Koa 带客户端路由器

javascript - 如何使用 NVD3 使独特的线条更细

javascript - Javascript 和 PHP 之间的简单交互

html - 标题菜单下拉菜单和图像不显示

python - 如何使用 HTML 从 Python (Django) 字符串中读取和打印带有换行符的文本?

html - 如何在不影响其他区域的情况下在CSS Grid中制作一个动态区域?

javascript - 网格的过滤器存储在两个不同的列上

javascript - 检查数字的反转是否等于数字

javascript - 链接在点击事件上在<li>上不起作用

javascript - 有序列表上的动画