Javascript - 根据所选图像更改图像

时间:2016-04-07 19:26:21

标签: javascript html asp.net

我正在使用asp.net mvc。

我有两个图标/图片如下:

<div class="gallery-preview-img" id="forest">
                                    <a href="#"><img onclick="changeImage()" src="~/Content/themes/base/images/forest_icon.png"></a>
                                </div>
<div class="gallery-preview-img" id="urban">
                                    <a href="#"><img onclick="changeImage()" src="~/Content/themes/base/images/urban_icon.png"></a>
                                </div>

脚本:

        $(function changeImage() {
        $('#forest').click(function () {
            if (!(document.getElementById('forest').src.match('forestG'))) {
                document.getElementById('forest').src = "~/Content/themes/base/images/forestG.png";
                if (!(document.getElementById('urban').src.match('urban_icon')) {
                    document.getElementById('urban').src = "~/Content/themes/base/images/urban_icon.png";
                }
            }
            document.getElementById('env').value = 'Forest';
        })
        $('#urban').click(function () {
            if (!(document.getElementById('urban').src.match('urbanG')) {
                document.getElementById('urban').src = "~/Content/themes/base/images/urbanG.png";
                if (!(document.getElementById('forest').src != 'forest_icon')) {
                    document.getElementById('forest').src = "~/Content/themes/base/images/forest_icon.png";
                }
            }
            document.getElementById('env').value = 'Urban';
        });
    }
 });

但是图片/图标没有变化。我有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是我认为你想要做的事情的链接。我认为你现在的做法有点太复杂了。希望这有帮助

https://jsfiddle.net/stevenkaspar/qs8L20wt/

boost::python